我尝试使用interpolate()方法在我的DataFrame中插入NaN .但是,该方法失败并出现错误:
无法插入所有NaN.
这是代码:
try:
df3.interpolate(method='index', inplace=True)
processor._arma(df3['TCA'])
except Exception, e:
sys.stderr.write('%s: [%s] %s\n' % (time.strftime("%Y-%m-%d %H:%M:%S"), nid3, e))
sys.stderr.write('%s: [%s] len=%d\n' % (time.strftime("%Y-%m-%d %H:%M:%S"), nid3, len(df3.index)))
sys.stderr.write('%s: [%s] %s\n' % (time.strftime("%Y-%m-%d %H:%M:%S"), nid3, df3.to_string()))
Run Code Online (Sandbox Code Playgroud)
这很奇怪,因为大多数数据已经填满,您可以在log 1或log 2中看到.数据帧的长度为20,如下所示的所有数据.即使每个单元格都被填充,我仍然无法使用插值方法.BTW,df3是一个全球价值,我不确定它是否会成为一个问题.
记录1
2016-01-21 22:06:11: [ESIG_node_003_400585511] Cannot interpolate with all NaNs.
2016-01-21 22:06:11: [ESIG_node_003_400585511] len=20
2016-01-21 22:06:11: [ESIG_node_003_400585511]
TCA TCB TCC
2016-01-21 20:06:22 19 17 18
2016-01-21 20:06:23 19 17 18
2016-01-21 20:06:24 18 18 18
2016-01-21 20:06:25 18 17 18
2016-01-21 20:06:26 18 18 18
2016-01-21 20:06:27 19 18 18
2016-01-21 20:06:28 19 17 18
2016-01-21 20:06:29 18 18 18
2016-01-21 20:06:30 18 17 18
2016-01-21 20:06:31 19 17 18
2016-01-21 20:06:32 18 17 18
2016-01-21 20:06:33 18 18 18
2016-01-21 20:06:34 19 18 18
2016-01-21 20:06:35 18 17 18
2016-01-21 20:06:36 19 18 18
2016-01-21 20:06:37 18 18 18
2016-01-21 20:06:38 18 18 18
2016-01-21 20:06:39 19 18 18
2016-01-21 20:06:40 18 17 18
2016-01-21 20:06:41 18 18 18
Run Code Online (Sandbox Code Playgroud)
记录2
2016-01-21 22:06:14: [ESIG_node_003_400585511] Cannot interpolate with all NaNs.
2016-01-21 22:06:14: [ESIG_node_003_400585511] len=20
2016-01-21 22:06:14: [ESIG_node_003_400585511]
TCA TCB TCC
2016-01-21 20:06:33 18 18 18
2016-01-21 20:06:34 19 18 18
2016-01-21 20:06:35 18 17 18
2016-01-21 20:06:36 19 18 18
2016-01-21 20:06:37 18 18 18
2016-01-21 20:06:38 18 18 18
2016-01-21 20:06:39 19 18 18
2016-01-21 20:06:40 18 17 18
2016-01-21 20:06:41 18 18 18
2016-01-21 20:06:42 NaN NaN NaN
2016-01-21 20:06:43 NaN NaN NaN
2016-01-21 20:06:44 NaN NaN NaN
2016-01-21 20:06:45 NaN NaN NaN
2016-01-21 20:06:46 19 18 18
2016-01-21 20:06:47 18 17 18
2016-01-21 20:06:48 18 18 18
2016-01-21 20:06:49 19 18 18
2016-01-21 20:06:50 18 17 18
2016-01-21 20:06:51 18 18 18
2016-01-21 20:06:52 19 17 18
Run Code Online (Sandbox Code Playgroud)
unu*_*tbu 20
检查您的DataFrame是否具有数字dtypes,而不是objectdtypes.该
TypeError: Cannot interpolate with all NaNs如果数据帧中包含的列可发生objectD型.例如,如果
import numpy as np
import pandas as pd
df = pd.DataFrame({'A':np.array([1,np.nan,30], dtype='O')},
index=['2016-01-21 20:06:22', '2016-01-21 20:06:23',
'2016-01-21 20:06:24'])
Run Code Online (Sandbox Code Playgroud)
然后df.interpolate()引发TypeError.
要检查您的DataFrame是否包含具有对象dtype的列,请查看df3.dtypes:
In [92]: df.dtypes
Out[92]:
A object
dtype: object
Run Code Online (Sandbox Code Playgroud)
要解决此问题,您需要确保DataFrame具有带有本机NumPy dtypes的数字列.显然,最好从一开始就正确构建DataFrame.因此,最佳解决方案取决于您如何构建DataFrame.
一个不太吸引人的补丁修复将用于pd.to_numeric在事后将对象数组转换为数字数组:
for col in df:
df[col] = pd.to_numeric(df[col], errors='coerce')
Run Code Online (Sandbox Code Playgroud)
使用时errors='coerce',任何无法转换为数字的值都将转换为NaN.在调用pd.to_numeric每一列后,请注意dtype现在是float64:
In [94]: df.dtypes
Out[94]:
A float64
dtype: object
Run Code Online (Sandbox Code Playgroud)
一旦DataFrame具有数字dtypes,并且DataFrame具有DatetimeIndex,那么df.interpolate(method='time')将工作:
import numpy as np
import pandas as pd
df = pd.DataFrame({'A':np.array([1,np.nan,30], dtype='O')},
index=['2016-01-21 20:06:22', '2016-01-21 20:06:23',
'2016-01-21 20:06:24'])
for col in df:
df[col] = pd.to_numeric(df[col], errors='coerce')
df.index = pd.DatetimeIndex(df.index)
df = df.interpolate(method='time')
print(df)
Run Code Online (Sandbox Code Playgroud)
产量
A
2016-01-21 20:06:22 1.0
2016-01-21 20:06:23 15.5
2016-01-21 20:06:24 30.0
Run Code Online (Sandbox Code Playgroud)