Skip to content

BUG: groupby get_group should be more datelike friendly #5267

Closed
@jreback

Description

@jreback

http://stackoverflow.com/questions/19458361/python-pandas-groupby-date-and-accessing-each-group-by-timestamp

In [2]: df= pd.DataFrame({'DATE' : ['10-Oct-2013', '10-Oct-2013', '10-Oct-2013', '11-Oct-2013', '11-Oct-2013', '11-Oct-2013'],'VAL' : [1,2,3,4,5,6]})

In [3]: df['DATE'] = pd.to_datetime(df['DATE'])

In [4]: df
Out[4]: 
                 DATE  VAL
0 2013-10-10 00:00:00    1
1 2013-10-10 00:00:00    2
2 2013-10-10 00:00:00    3
3 2013-10-11 00:00:00    4
4 2013-10-11 00:00:00    5
5 2013-10-11 00:00:00    6

In [5]: g = df.groupby('DATE')

In [8]: key = g.groups.keys()[0]

In [9]: key
Out[9]: numpy.datetime64('2013-10-09T20:00:00.000000000-0400')

In [10]: g.indices
Out[10]: 
{1381363200000000000L: array([0, 1, 2]),
 1381449600000000000L: array([3, 4, 5])}

In [11]: g.get_group(key.astype('i8'))
Out[11]: 
                 DATE  VAL
0 2013-10-10 00:00:00    1
1 2013-10-10 00:00:00    2
2 2013-10-10 00:00:00    3

so works, but not very friendly
in addition on windows, the following is needed for the selection to succeed

g.get_group(long(key.astype('i8')))

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions