【发布时间】:2021-07-26 08:13:50
【问题描述】:
我有一个数据框 (df),其中包含一个名为“日期”的变量,我使用以下代码对其进行索引:
idx = df.groupby(pd.PeriodIndex(data=df.date, freq='10min'))
df = idx.sum()
因为我的数据中有“漏洞”,我想用 NaN 填充,所以我想使用名为 ts 的完整时间序列重新索引此数据帧:
print(ts)
array(['2014-08-19T00:00:00.000000000', '2014-08-19T00:10:00.000000000', '2014-08-19T00:20:00.000000000', ..., '2015-08-16T23:40:00.000000000', '2015-08-16T23:50:00.000000000', '2015-08-17T00:00:00.000000000'], dtype='datetime64[ns]')
为了重新索引,我使用以下代码:
df = df.reindex(ts).copy()
但收到以下错误消息:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-160-2c1b1d883eb1> in <module>()
----> 1 bi = bi.reindex(index)
10 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/arrays/period.py in dt64arr_to_periodarr(data, freq, tz)
952 data, freq = data._values, data.dt.freq
953
--> 954 freq = Period._maybe_convert_freq(freq)
955
956 if isinstance(data, (ABCIndexClass, ABCSeries)):
pandas/_libs/tslibs/period.pyx in pandas._libs.tslibs.period._Period._maybe_convert_freq()
AttributeError: 'NoneType' object has no attribute 'n'
有人可以向我解释为什么我会收到此消息吗?谢谢
这是 df 的一部分:
date aa bb cc
2014-08-19 00:00 781 9.798 9.289
2014-08-19 00:10 782 10.004 9.382
2014-08-19 00:20 783 9.832 9.434
2014-08-19 00:30 784 10.019 9.593
2014-08-19 00:40 785 10.087 10.028
【问题讨论】:
-
你能发布 df 以便我们也可以运行它
-
完成。感谢您对我的问题感兴趣!
标签: pandas dataframe datetime indexing