滚动时间序列数据:Nan issue

Rolling time series data: Nan issue

我有一个时间序列数据集,目前我处理得不是很好。

情节有所改进,但它仍然没有很好地使用标签 space.. 所以现在我分享没有它的情节,因为我想稍后解决可视化问题..

时间序列数据图:

代码:

dir = sorted(glob.glob("bsrn_txt_0100/*.txt"))
gen_raw = (pd.read_csv(file, sep='\t', encoding = "utf-8") for file in dir)
gen = pd.concat(gen_raw, ignore_index=True)
gen.drop(gen.columns[[1,2]], axis=1, inplace=True)

#gen['Date/Time'] = gen['Date/Time'][11:] -> cause error, didnt work
filter = gen[gen['Date/Time'].str.endswith('00') | gen['Date/Time'].str.endswith('30')]
filter['rad_tot'] = filter['Direct radiation [W/m**2]'] + filter['Diffuse radiation [W/m**2]']
filter['Date/Time'] = filter['Date/Time'].str.replace('T', ' ')
filter['Date/Time'] = pd.to_datetime(filter['Date/Time'])

df = filter.filter(['Date/Time', 'rad_tot']).copy()
df = df.set_index('Date/Time')
print(df)
plot_df = df.rolling(window=12).mean().fillna(0)
print(plot_df)
plot_df.plot()

输出:

当前问题:

您正在使用

plot_df=df.rolling(window=12).mean()

这为您提供了最后 12 分的平均值。因为对于前 11 个值,无法计算这些值,因此会产生 'na'.

plot_df.fillna(0)

这会将 na 替换为 0。

您还可以从数据框中删除前 11 个值,这样就不会在左侧出现空白。

plot_df[:10].plot()

或者您计算滚动平均值并忽略绘图中的 na 值以去除左侧和右侧的空白:

df=df.rolling(window=12).mean()
df.dropna().plot()