pandas 如何将 DatetimeIndexResampler 转换为 DataFrame?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39492004/
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
How to convert DatetimeIndexResampler to DataFrame?
提问by displayname
I want to build a matrix from series but before that I have to resample those series. However, to avoid processing the whole matrix twice with replace(np.nan, 0.0)
I want to append the dataframes to a collecting dataframe and then remove NaN
values in one pass.
我想从系列构建一个矩阵,但在此之前我必须重新采样这些系列。但是,为了避免处理整个矩阵两次,replace(np.nan, 0.0)
我想将数据帧附加到收集数据帧,然后NaN
一次性删除值。
So instead of
所以代替
user_activities = user.groupby(["DOC_ACC_DT", "DOC_ACTV_CD"]).agg("sum")["SUM_DOC_CNT"].unstack().resample("1D").replace(np.nan, 0)
df = df.append(user_activities[activity].rename(user_id))
I want
我想要
user_activities = user.groupby(["DOC_ACC_DT", "DOC_ACTV_CD"]).agg("sum")["SUM_DOC_CNT"].unstack().resample("1D")
df = df.append(user_activities[activity].rename(user_id))
but that is not working because user_activities
is not a dataframe after resample()
.
但这不起作用,因为user_activities
在resample()
.
The error suggests that I try apply()
but that method expects a parameter:
该错误表明我尝试apply()
但该方法需要一个参数:
/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.pyc in _make_wrapper(self, name)
507 "using the 'apply' method".format(kind, name,
508 type(self).__name__))
--> 509 raise AttributeError(msg)
510
511 # need to setup the selection
AttributeError: Cannot access callable attribute 'rename' of 'SeriesGroupBy' objects, try using the 'apply' method
How can I solve this issue?
我该如何解决这个问题?
回答by scls
The interface to .resample
has changed in Pandas 0.18.0 to be more groupby-like and hence more flexible ie resample
no longer returns a DataFrame: it's now "lazyly evaluated" at the moment of the aggregation or interpolation.
.resample
Pandas 0.18.0 中的接口已更改为更像 groupby,因此更灵活,即resample
不再返回 DataFrame:它现在在聚合或插值时“惰性求值”。
I suggest reading resample API changes http://pandas.pydata.org/pandas-docs/stable/whatsnew.html#resample-api
我建议阅读重采样 API 更改http://pandas.pydata.org/pandas-docs/stable/whatsnew.html#resample-api
See also:
也可以看看:
http://pandas.pydata.org/pandas-docs/stable/timeseries.html#resampling
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.resample.html
http://pandas.pydata.org/pandas-docs/stable/timeseries.html#resampling
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.resample.html
for upscaling
升级
df.resample("1D").interpolate()
for downscaling
用于缩小规模
using mean
使用平均值
df.resample("1D").mean()
using OHLC
使用 OHLC
ie open high low close values or first maximal minimal last values
即打开高低关闭值或第一个最大最小最后一个值
df.resample("1D").ohlc()