Python 熊猫如何使用列表作为分组标准,对日期时间序列执行groupby?

Python 熊猫如何使用列表作为分组标准,对日期时间序列执行groupby?,python,pandas,datetime,dataframe,Python,Pandas,Datetime,Dataframe,如果df是由DateTime对象索引的Dataframe,则以下代码将其拆分为列表groups\u list,其中每个索引包含df中属于给定日期的所有数据: groupby_clause = [df.index.year,df.index.month,df.index.day] groups_list = [group[1] for group in df.groupby(groupby_clause)] 但是,我很难理解分组是如何进行的,因为我不需要将groupby_子句的元素标记为年、月和

如果
df
是由
DateTime
对象索引的
Dataframe
,则以下代码将其拆分为列表
groups\u list
,其中每个索引包含
df
中属于给定日期的所有数据:

groupby_clause = [df.index.year,df.index.month,df.index.day]
groups_list = [group[1] for group in df.groupby(groupby_clause)]
但是,我很难理解分组是如何进行的,因为我不需要将
groupby_子句的元素标记为年、月和日,以便在
DateTime
对象上进行分组

例如,我为
组列表
提供了以下组件:


也许我遗漏了一些明显的东西,但我不明白:熊猫如何知道它应该将
groupby_子句[0]
与年份关联,
groupby_子句[1]
与月份关联,以及
groupby_子句[2]
to day以便对具有日期时间类型的数据帧索引进行分组?

假设您有这样一个数据帧:

                            0
2011-01-01 00:00:00 -0.324398
2011-01-01 01:00:00 -0.761585
2011-01-01 02:00:00  0.057204
2011-01-01 03:00:00 -1.162510
2011-01-01 04:00:00 -0.680896
2011-01-01 05:00:00 -0.701835
2011-01-01 06:00:00 -0.431338
2011-01-01 07:00:00  0.306935
2011-01-01 08:00:00 -0.503177
2011-01-01 09:00:00 -0.507444
2011-01-01 10:00:00  0.230590
2011-01-01 11:00:00 -2.326702
2011-01-01 12:00:00 -0.034664
2011-01-01 13:00:00  0.224373
2011-01-01 14:00:00 -0.242884
如果您希望索引按年份、月份和日期显示,则只需
set\u index
it:

df.set_index([ts.index.year, ts.index.month, ts.index.day])
输出
                 0
2011 1 1 -0.324398
       1 -0.761585
       1  0.057204
       1 -1.162510
       1 -0.680896
       1 -0.701835
       1 -0.431338
       1  0.306935
       1 -0.503177
       1 -0.507444
       1  0.230590
       1 -2.326702
       1 -0.034664
       1  0.224373
       1 -0.242884
       1 -0.134757
       1 -1.177362
       1  0.931335
       1  0.904084
       1 -0.757860
       1  0.406597
       1 -0.664150