Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename multiindex bug in DataFrame #4023

Closed
joeb1415 opened this issue Jun 25, 2013 · 6 comments
Closed

rename multiindex bug in DataFrame #4023

joeb1415 opened this issue Jun 25, 2013 · 6 comments
Labels
Milestone

Comments

@joeb1415
Copy link

related / same as #4160

from datetime import datetime, time
from pandas import date_range, Series, DataFrame

time_ind = date_range(datetime(2013, 1, 1), datetime(2013, 1, 10), freq='1min')

s = Series(1, index=time_ind)
s_g = s.groupby([lambda x: x.hour, lambda x: x.minute]).sum()
s_g = s_g.rename(lambda (h, m): time(h, m, 0))

df = DataFrame(1, index=time_ind, columns=['a', 'b'])
df_g = df.groupby([lambda x: x.hour, lambda x: x.minute]).sum()
df_g = df_g.rename(lambda (h, m): time(h, m, 0))

Works fine on Series, but throws error on DataFrame:

Traceback (most recent call last):

File "<ipython-input-25-4e51131ae70d>", line 2, in <module>
  df_g = df_g.rename(lambda (h, m): time(h, m, 0))

File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 3539, in rename
  result._rename_index_inplace(index_f)

File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 3548, in _rename_index_inplace
  self._data = self._data.rename_axis(mapper, axis=1)

File "C:\Python27\lib\site-packages\pandas\core\internals.py", line 1734, in rename_axis
  new_axis = MultiIndex.from_tuples([tuple(mapper(y) for y in x) for x in index], names=index.names)

File "C:\Python27\lib\site-packages\pandas\core\internals.py", line 1734, in <genexpr>
  new_axis = MultiIndex.from_tuples([tuple(mapper(y) for y in x) for x in index], names=index.names)

File "<ipython-input-25-4e51131ae70d>", line 2, in <lambda>
  df_g = df_g.rename(lambda (h, m): time(h, m, 0))

TypeError: 'long' object is not iterable
@jreback
Copy link
Contributor

jreback commented Jun 25, 2013

do it like this

In [32]: df_g.index = map(lambda (h, m): time(h, m, 0),df_g.index)

In [33]: df_g
Out[33]: 
<class 'pandas.core.frame.DataFrame'>
Index: 1440 entries, 00:00:00 to 23:59:00
Data columns (total 2 columns):
a    1440  non-null values
b    1440  non-null values
dtypes: int64(2)

@joeb1415
Copy link
Author

This works. Thanks.
Any reason DataFrame.rename doesn't / shouldn't work too ?

@jreback
Copy link
Contributor

jreback commented Jun 25, 2013

its a bug sort of of; using objects as column labels is frowned upon, but sometimes works depending on the repr. Pandas really doesn't have a straight time index, so I understand what you are doing; going to mark it as a feature request.

Its a bug because I think the time is somehow misinterpreted as a multi-index tuple.....

@jreback
Copy link
Contributor

jreback commented Jun 25, 2013

@wesm reason to have a TimeIndex?

@wesm
Copy link
Member

wesm commented Jun 25, 2013

might make sense. at some point we'll need to unify the column/index array business (i.e. if you want to call datetimeindex methods on a datetime64 column, you have to convert it first)

@jreback
Copy link
Contributor

jreback commented Sep 8, 2014

same as #4160

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants