简体   繁体   English

大熊猫如何通过使用列表作为分组条件对DateTime系列执行分组依据?

[英]How does pandas perform a groupby on a DateTime series by using a list as grouping criteria?

If df is a Dataframe indexed by DateTime objects, the following code splits it into the list groups_list where each index containts all the data in df that belongs to a given day: 如果df是由DateTime对象索引的数据Dataframe ,则以下代码将其拆分为列表groups_list ,其中每个索引包含df中属于给定日期的所有数据:

groupby_clause = [df.index.year,df.index.month,df.index.day]
groups_list = [group[1] for group in df.groupby(groupby_clause)]

I am having trouble, though, to understand how the grouping is actually made, since I don't need to label the elements of groupby_clause as year, month, and day for the grouping to be made on DateTime objects. 不过,在理解分组的实际方式时遇到了麻烦,因为我不需要将groupby_clause的元素groupby_clause为年,月和日,以便在DateTime对象上进行分组。

As an example, I have the following components for groups_list : 举例来说,我具有groups_list的以下组件:

在此处输入图片说明

Maybe I'm missing something obvious, but I don't get it: how does pandas know that it should associate groupby_clause[0] to year, groupby_clause[1] to month, and groupby_clause[2] to day in order to group the dataframe indexes that have DateTime type? 也许我缺少明显的东西,但是我不明白:大熊猫如何知道应该将groupby_clause[0]关联到年份,将groupby_clause[1]关联到月份,将groupby_clause[2]到一天,以便对具有DateTime类型的数据帧索引?

Suppose you have a DataFrame like this: 假设您有一个这样的DataFrame:

                            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

If you want the index to be by year month and date then just set_index it: 如果您希望索引按年份月份和日期set_index ,则只需set_index

df.set_index([ts.index.year, ts.index.month, ts.index.day])

Output 产量

                 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM