如何将DatetimeIndexResampler转换为DataFrame? [英] How to convert DatetimeIndexResampler to DataFrame?

查看:485
本文介绍了如何将DatetimeIndexResampler转换为DataFrame?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从系列中构建一个矩阵,但是在此之前,我必须对这些系列进行重新采样.但是,为了避免使用replace(np.nan, 0.0)两次处理整个矩阵,我想将数据帧附加到收集的数据帧中,然后一次性删除NaN值.

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.

所以不是

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))

我想要

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))

但这不起作用,因为user_activities不是resample()之后的数据帧.

but that is not working because user_activities is not a dataframe after resample().

该错误表明我尝试了apply(),但是该方法需要一个参数:

The error suggests that I try apply() but that method expects a parameter:

/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?

推荐答案

.resample的接口在Pandas 0.18.0中已更改,变得更加分组化,因此更加灵活,即resample不再返回DataFrame:现在,它在汇总或插值时会懒惰地评估".

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.

我建议阅读重新采样API更改 http://pandas. pydata.org/pandas-docs/stable/whatsnew.html#resample-api

I suggest reading resample API changes http://pandas.pydata.org/pandas-docs/stable/whatsnew.html#resample-api

另请参阅:

http://pandas.pydata .org/pandas-docs/stable/generation/pandas.DataFrame.resample.html

df.resample("1D").interpolate()

缩小尺寸

使用均值

df.resample("1D").mean()

使用OHLC

即开高,低收盘价或第一个最大,最小,最后一个值

using OHLC

ie open high low close values or first maximal minimal last values

df.resample("1D").ohlc()

这篇关于如何将DatetimeIndexResampler转换为DataFrame?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆