如何处理熊猫中仅包含NaN值的单元格?

时间:2019-04-13 15:10:01

标签: python pandas time-series

我正在设置股票价格预测数据集,同时为Ichimoku Cloud Indicator应用以下代码:

from datetime import timedelta
high_9 = df['High'].rolling(window= 9).max()
low_9 = df['Low'].rolling(window= 9).min()
df['tenkan_sen'] = (high_9 + low_9) /2

high_26 = df['High'].rolling(window= 26).max()
low_26 = df['Low'].rolling(window= 26).min()
df['kijun_sen'] = (high_26 + low_26) /2

# this is to extend the 'df' in future for 26 days
# the 'df' here is numerical indexed df
# the problem is here
last_index = df.iloc[-1:].index[0]
last_date = df['Date'].iloc[-1].date()
for i in range(26):
    df.loc[last_index+1 +i, 'Date'] = last_date + timedelta(days=i)

df['senkou_span_a'] = ((df['tenkan_sen'] + df['kijun_sen']) / 2).shift(26)

high_52 = df['High'].rolling(window= 52).max()
low_52 = df['Low'].rolling(window= 52).min()
df['senkou_span_b'] = ((high_52 + low_52) /2).shift(26)

# most charting softwares dont plot this line
df['chikou_span'] = df['Close'].shift(-26)   

上面的代码很好用,但是问题是,当扩展到“ senoku span a”和“ b”列中的下一个26个时间步(行)时,它将其他其余列的值转换为NaN。

因此,我需要帮助来使我的数据集中的'Senoku span a'和'Senoku span b'预测行不致使其他行对NaN产生影响。

当前输出为:

Date      Open    High     Low   Close   Senoku span a    Senoku span b
2019-03-16 50      51       52     53      56.0               55.82
2019-03-17 NaN     NaN     NaN     NaN     55.0               56.42
2019-03-18 NaN     NaN     NaN     NaN     54.0               57.72
2019-03-19 NaN     NaN     NaN     NaN     53.0               58.12
2019-03-20 NaN     NaN     NaN     NaN     52.0               59.52

预期输出为:

Date      Open    High     Low   Close   Senoku span a    Senoku span b
2019-03-16  50      51       52     53     56.0               55.82
2019-03-17                                 55.0               56.42
2019-03-18                                 54.0               57.72
2019-03-19                                 53.0               58.12
2019-03-20                                 52.0               59.52

0 个答案:

没有答案
相关问题