[英]pandas: where is the documentation for TimeGrouper?
I use Pandas
a lot and its great.我经常使用Pandas
,它很棒。 I use TimeGrouper
as well, and its great.我也使用TimeGrouper
,它很棒。 I actually dont know where is the documentation about TimeGrouper
.我实际上不知道有关TimeGrouper
的文档在哪里。 Is there any?有没有?
Thanks!谢谢!
pd.TimeGrouper()
was formally deprecated in pandas v0.21.0 in favor of pd.Grouper()
. pd.TimeGrouper()
在 pandas v0.21.0 中被正式弃用,取而代之的是pd.Grouper()
。
The best use of pd.Grouper()
is within groupby()
when you're also grouping on non-datetime-columns.当您还在非日期时间列上进行分组时, pd.Grouper()
的最佳用途是在groupby()
内。 If you just need to group on a frequency, use resample()
.如果您只需要按频率分组,请使用resample()
。
For example, say you have:例如,假设您有:
>>> import pandas as pd
>>> import numpy as np
>>> np.random.seed(444)
>>> df = pd.DataFrame({'a': np.random.choice(['x', 'y'], size=50),
'b': np.random.rand(50)},
index=pd.date_range('2010', periods=50))
>>> df.head()
a b
2010-01-01 y 0.959568
2010-01-02 x 0.784837
2010-01-03 y 0.745148
2010-01-04 x 0.965686
2010-01-05 y 0.654552
You could do:你可以这样做:
>>> # `a` is dropped because it is non-numeric
>>> df.groupby(pd.Grouper(freq='M')).sum()
b
2010-01-31 18.5123
2010-02-28 7.7670
But the above is a little unnecessary because you're only grouping on the index.但是以上内容有点不必要,因为您只是在索引上进行分组。 Instead you could do:相反,你可以这样做:
>>> df.resample('M').sum()
b
2010-01-31 16.168086
2010-02-28 9.433712
to produce the same result.产生相同的结果。
Conversely, here's a case where Grouper()
would be useful:相反,这是Grouper()
有用的情况:
>>> df.groupby([pd.Grouper(freq='M'), 'a']).sum()
b
a
2010-01-31 x 8.9452
y 9.5671
2010-02-28 x 4.2522
y 3.5148
For some more detail, take a look at Chapter 7 of Ted Petrou's Pandas Cookbook .有关更多详细信息,请查看 Ted Petrou 的Pandas Cookbook的第 7 章。
pandas.TimeGrouper()
was deprecated in favour of pandas.Grouper()
in pandas v0.21. pandas.TimeGrouper()
) 在 pandas v0.21 中被弃用,取而代之的是pandas.Grouper()
。
Use pandas.Grouper()
instead.请改用pandas.Grouper()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.