-
-
Notifications
You must be signed in to change notification settings - Fork 19.1k
Description
Code Sample, a copy-pastable example if possible
field = 'dt_' # Requires a '_' for bug to occur
import pandas as pd
from pandas import Timestamp
from datetime import datetime as dt
df = pd.DataFrame({u'ccy': {0: 'EUR', 1: 'USD', 2: 'USD'}, u'spam': {0: 10000000, 1: 10000000, 2: 123}, field: {0: Timestamp('2016-02-01 00:00:00'), 1: Timestamp('2016-02-01 00:00:00'), 2: Timestamp('2016-02-02 00:00:00')}}).set_index(field)
fx = pd.DataFrame([[dt(2016,2,1), 1.25], [dt(2016,2,2), 1.26]], columns=['dt', 'fx']).set_index('dt')
def foo(k):
return k.merge(fx, left_index=True, right_index=True)
# Always ok
df.groupby('ccy').apply(foo)
# Not okay if `'_' in field`
df[:1].groupby('ccy').apply(foo)
df[:2].groupby('ccy').apply(foo)
Problem description
Doing df.groupby('ccy').apply(foo)
should maintain its index name. Indeed, if df
's index contains more than 1 unique entry (i.e. len(set(df.index)) > 1
, this always works.
In [22]: df.groupby('ccy').apply(foo)
Out[22]:
ccy spam fx
ccy dt_
EUR 2016-02-01 EUR 10000000 1.25
USD 2016-02-01 USD 10000000 1.25
2016-02-02 USD 123 1.26
However, if df
's index
contains only a unique entry, and if the index name contains a _
character, the output's index name is dropped.
In [26]: df[:2].groupby('ccy').apply(foo)
Out[26]:
ccy spam fx
ccy
EUR 2016-02-01 EUR 10000000 1.25
USD 2016-02-01 USD 10000000 1.25
(re-run the code sample, with first line replaced with field = 'dt'
, and the bug will never occur.)
Output of pd.show_versions()
pandas: 0.19.2
nose: None
pip: 9.0.1
setuptools: 35.0.2
Cython: None
numpy: 1.12.1
scipy: None
statsmodels: None
xarray: None
IPython: 5.3.0
sphinx: None
patsy: None
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
boto: None
pandas_datareader: None