【发布时间】:2020-01-14 16:13:45
【问题描述】:
我想转发一个每小时的数据帧,以便在接下来的几天中,每小时 1 的值都会被转发。每 24 小时都一样。
数据框如下所示:
Timestamp input1 input2 input3
… … … ..
01.01.2018 00:00 2 5 4
01.01.2018 01:00 3 3 2
01.01.2018 02:00 5 6 1
…
01.01.2018 22:00 2 0 1
01.01.2018 23:00 5 3 3
02.01.2018 00:00 6 2 5
02.01.2018 01:00 3 6 4
02.01.2018 02:00 3 9 6
02.01.2018 03:00 5 1 7
…
02.01.2018 23:00 2 5 1
03.01.2018 00:00 NaN NaN NaN
…
03.01.2018 23:00 NaN NaN NaN
我为此使用以下代码:
for hr in range(0,24):
df.loc[df.index.hour == hr, Inputs] = df.loc[df.index.hour == hr, Inputs].fillna(method='ffill')
这行得通。 不幸的是,我收到了一条警告消息:
\Python\WPy-3670_32bit\python-3.6.7\lib\site-packages\pandas\core\indexing.py:543: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self.obj[item] = s
我该如何解决这个问题,我不再收到警告?
生成的 df 应该填充了 NaN。
【问题讨论】:
-
这通常意味着你的数据框是另一个数据框的一部分。你在某个地方有
df = other_df.loc[...]吗?
标签: python-3.x pandas dataframe time-series warnings