Skip to content

error with groupby and transform while using datetime64[ns] as part of a key #6303

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

Closed
MiloszD opened this issue Feb 8, 2014 · 2 comments
Closed
Labels
Bug Duplicate Report Duplicate issue or pull request Groupby

Comments

@MiloszD
Copy link

MiloszD commented Feb 8, 2014

from pandas.util.testing import choice
from pandas import DataFrame, date_range
from numpy.random import randn

n = 8
dates = date_range('1/1/2000', periods=n, freq='1T')
colors = choice(['red', 'green'], size=n)

df = DataFrame({'value': randn(n), 'date': dates, 'color': colors})

zscore = lambda x: (x - x.mean()) / x.std()

df
Out[2]:
color date value
0 red 2000-01-01 00:00:00 -0.382902
1 green 2000-01-01 00:01:00 0.991723
2 green 2000-01-01 00:02:00 -0.442476
3 green 2000-01-01 00:03:00 -0.776372
4 red 2000-01-01 00:04:00 -1.372511
5 red 2000-01-01 00:05:00 -0.276006
6 red 2000-01-01 00:06:00 -0.182162
7 red 2000-01-01 00:07:00 -0.232967

[8 rows x 3 columns]

df.dtypes
Out[3]:
color object
date datetime64[ns]
value float64
dtype: object

df.groupby(['color','date'])['value'].transform(zscore)


KeyError Traceback (most recent call last)
in ()
----> 1 df.groupby(['color','date'])['value'].transform(zscore)

C:\Users\MD\Anaconda\lib\site-packages\pandas-0.13.1.dev-py2.7-win-amd64.egg\pandas\core\groupby.pyc in transform(self, func, _args, *_kwargs)
1845 # this needs to be an ndarray
1846 result = Series(result)
-> 1847 result.iloc[self._get_index(name)] = res
1848 result = result.values
1849

C:\Users\MD\Anaconda\lib\site-packages\pandas-0.13.1.dev-py2.7-win-amd64.egg\pandas\core\groupby.pyc in _get_index(self, name)
265 """ safe get index """
266 try:
--> 267 return self.indices[name]
268 except:
269 if isinstance(name, Timestamp):

KeyError: ('green', Timestamp('2000-01-01 00:01:00', tz=None))

@jreback
Copy link
Contributor

jreback commented Feb 8, 2014

this is a dup of #3794

@jreback jreback closed this as completed Feb 8, 2014
@jreback
Copy link
Contributor

jreback commented Feb 8, 2014

you can do a nested group or resample followed by a groupby as a work around

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Duplicate Report Duplicate issue or pull request Groupby
Projects
None yet
Development

No branches or pull requests

2 participants