Python pandas:TimeGrouper 的文档在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45156289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pandas: where is the documentation for TimeGrouper?
提问by ??????
I use Pandas
a lot and its great. I use TimeGrouper
as well, and its great. I actually dont know where is the documentation about TimeGrouper
. Is there any?
我用Pandas
了很多,它很棒。我TimeGrouper
也在用,很好用。我实际上不知道关于TimeGrouper
. 有没有?
Thanks!
谢谢!
采纳答案by Brad Solomon
pd.TimeGrouper()
was formally deprecatedin 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. If you just need to group on a frequency, use resample()
.
最好的用途pd.Grouper()
是在groupby()
您还对非日期时间列进行分组时。如果您只需要按频率分组,请使用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 coulddo:
你可以这样做:
>>> # `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 章。
回答by rth
pandas.TimeGrouper()
was deprecatedin favour of pandas.Grouper()
in pandas v0.21.
pandas.TimeGrouper()
被弃用赞成pandas.Grouper()
在大熊猫v0.21。
Use pandas.Grouper()
instead.
使用pandas.Grouper()
来代替。