Python Pandas: Groupby date, and accessing each group by timestamp

notilas

I want to groupby timestamp (date) and access each group by timestamp, which looks not working properly. It looks like the group keys are strangely indexed with different formats.

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]})

>>> df
          DATE  VAL
0  10-Oct-2013    1
1  10-Oct-2013    2
2  10-Oct-2013    3
3  11-Oct-2013    4
4  11-Oct-2013    5
5  11-Oct-2013    6


dfg=df.groupby(df['DATE'].apply(lambda x: pd.to_datetime(x)))

>>> dfg.groups.keys()
[numpy.datetime64('NaT'), numpy.datetime64('2013-10-10T17:00:00.000000000-0700'), numpy.datetime64('2013-10-09T17:00:00.000000000-0700')]

for d in dfg.groups.keys():
    try:
        print d,dfg.get_group(d).describe()
    except:
        print 'err'
>>
NaT err
2013-10-10T17:00:00.000000000-0700 err
2013-10-09T17:00:00.000000000-0700 err

rng = pd.to_datetime(pd.date_range('10/10/2013', periods=3, freq='D'))

for d in rng:
    try:
        print d,dfg.get_group(d).describe()
    except:
        print 'err'

2013-10-10 00:00:00 err
2013-10-11 00:00:00 err
2013-10-12 00:00:00 err
Jeff

Here's your frame

In [40]: 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]})

Much faster to directly convert a date-like column

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

In [42]: df.dtypes
Out[42]: 
DATE    datetime64[ns]
VAL              int64
dtype: object

In [43]: df
Out[43]: 
                 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

This accomplishes what it loooks like you want

In [44]: df.groupby('DATE').describe()
Out[44]: 
                  VAL
DATE                 
2013-10-10 count  3.0
           mean   2.0
           std    1.0
           min    1.0
           25%    1.5
           50%    2.0
           75%    2.5
           max    3.0
2013-10-11 count  3.0
           mean   5.0
           std    1.0
           min    4.0
           25%    4.5
           50%    5.0
           75%    5.5
           max    6.0

If you REALLY want to get by a group individually

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

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

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

In [48]: g.get_group(key.astype('i8'))
Out[48]: 
                 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

datetime64[ns] are stored internally as long integers, so that's how they need to be accessed You normally really have no reason to do this as you can just

df.groupby('DATE').apply(lambda x: .....)

or if you really want to iterate

for g, grp in df.groupby('DATE'):
        ......

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python Pandas: Groupby date, and accessing each group by timestamp

From Dev

How do I get the first timestamp (index) of a group when applying groupby to a python pandas dataframe?

From Dev

Sample each group after pandas groupby

From Dev

Pandas groupby apply customized function to each group

From Dev

Pandas loop over groupby and plot each group

From Dev

Pandas GroupBy date range depending on each row

From Dev

python pandas groupby for first date

From Dev

python pandas groupby for first date

From Dev

Python pandas: extract date and time from timestamp

From Java

Get statistics for each group (such as count, mean, etc) using pandas GroupBy?

From Dev

how to apply different functions to each group of pandas groupby?

From Dev

Pandas groupby - apply different functions to half the records in each group

From Dev

How to create a data frame for each group in the pandas.groupby function?

From Dev

Returning subset of each group from a pandas groupby object

From Dev

Selecting top n elements from each group in pandas groupby

From Dev

Selecting top n elements from each group in pandas groupby

From Dev

python pandas groupby plot with sorted date as xtick

From Dev

add serial count to each group pandas python

From Dev

Python & Pandas - Group by day and count for each day

From Dev

mark last record in group by date pandas python

From Dev

How to add a date column in pandas that increases one hour each row given a start date Timestamp?

From Dev

Group by the date part of a timestamp field

From Dev

How to group by a date with a timestamp? (AngularJS)

From Dev

Pandas groupby date

From Dev

Pandas GroupBy Date Chunks

From Dev

Convert JSON timestamp string into python date in pandas dataframe

From Dev

for each group by date in coffeescript

From Dev

Python Pandas, setting groupby() group labels as index in a new dataframe

From Dev

Python Pandas, slice rows from group in .groupby().apply()

Related Related

  1. 1

    Python Pandas: Groupby date, and accessing each group by timestamp

  2. 2

    How do I get the first timestamp (index) of a group when applying groupby to a python pandas dataframe?

  3. 3

    Sample each group after pandas groupby

  4. 4

    Pandas groupby apply customized function to each group

  5. 5

    Pandas loop over groupby and plot each group

  6. 6

    Pandas GroupBy date range depending on each row

  7. 7

    python pandas groupby for first date

  8. 8

    python pandas groupby for first date

  9. 9

    Python pandas: extract date and time from timestamp

  10. 10

    Get statistics for each group (such as count, mean, etc) using pandas GroupBy?

  11. 11

    how to apply different functions to each group of pandas groupby?

  12. 12

    Pandas groupby - apply different functions to half the records in each group

  13. 13

    How to create a data frame for each group in the pandas.groupby function?

  14. 14

    Returning subset of each group from a pandas groupby object

  15. 15

    Selecting top n elements from each group in pandas groupby

  16. 16

    Selecting top n elements from each group in pandas groupby

  17. 17

    python pandas groupby plot with sorted date as xtick

  18. 18

    add serial count to each group pandas python

  19. 19

    Python & Pandas - Group by day and count for each day

  20. 20

    mark last record in group by date pandas python

  21. 21

    How to add a date column in pandas that increases one hour each row given a start date Timestamp?

  22. 22

    Group by the date part of a timestamp field

  23. 23

    How to group by a date with a timestamp? (AngularJS)

  24. 24

    Pandas groupby date

  25. 25

    Pandas GroupBy Date Chunks

  26. 26

    Convert JSON timestamp string into python date in pandas dataframe

  27. 27

    for each group by date in coffeescript

  28. 28

    Python Pandas, setting groupby() group labels as index in a new dataframe

  29. 29

    Python Pandas, slice rows from group in .groupby().apply()

HotTag

Archive