Python 3.x 数据帧-设置为复制警告
我希望forwardfill每小时数据帧,以便在接下来的几天中,每小时1的值都会得到forwardfill。24小时内的每一小时都相同 数据帧如下所示:Python 3.x 数据帧-设置为复制警告,python-3.x,pandas,dataframe,time-series,warnings,Python 3.x,Pandas,Dataframe,Time Series,Warnings,我希望forwardfill每小时数据帧,以便在接下来的几天中,每小时1的值都会得到forwardfill。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
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[df.index.hour == hr] = df[df.index.hour == hr].fillna(method="ffill")
与.loc非常相似,但不倾向于使用复制警告引发那么多设置。我执行了您的代码,但没有收到提到的警告(其他警告也没有) 使用loc是避免此类警告的正确方法(如本文所述) 也许你正在使用一些旧版本的熊猫? 如果您的版本较旧,请升级到0.25,然后重试 另一个怀疑:也许这个警告与某些其他指令有关
在您的代码中(没有loc)?通常意味着您的数据帧是另一个数据帧的一部分。你有没有
df=other_df.loc[…]
在什么地方?