将数据重新采样为每周数据?

时间:2020-04-15 15:27:28

标签: python pandas dataframe time-series resampling

我需要从每日格式到每周格式重新采样我的库存数据。 数据:

Date        Open            High            Low             Close
2007-09-17  4518.450195     4549.049805     4482.850098     4494.649902
2007-09-18  4494.100098     4551.799805     4481.549805     4546.200195
2007-09-19  4550.250000     4739.000000     4550.250000     4732.350098
2007-09-20  4734.850098     4760.850098     4721.149902     4747.549805
2007-09-21  4752.950195     4855.700195     4733.700195     4837.549805

数据缺少一些值,并已使用SimpleImputer估算

我已将此代码用于重新采样:

agg_dict = {'Open': 'first', 'High': 'max', 'Low': 'min', 'Close': 'last'}
r_df = nifty_tr.resample('W-MON').agg(agg_dict)

我似乎要面对的问题是输出数据集r_df包含各种新日期(从给定数据中可用的2007年年初开始,而不是2007-09-17年开始)。 Outout主要包含带有零位值的空值。 输出

Date        Open            High            Low             Close
2007-01-21  5021.500000     6011.950195     5001.350098     5866.450195
2007-01-28  NaN     NaN     NaN     NaN
2007-02-04  NaN     NaN     NaN     NaN
2007-02-11  NaN     NaN     NaN     NaN
2007-02-18  5854.850098     5944.750000     5714.250000     5932.399902
2020-11-15  10334.299810    10545.099610    10334.000000    10458.400390
......
2020-11-22  NaN     NaN     NaN     NaN
2020-11-29  NaN     NaN     NaN     NaN
2020-12-06  NaN     NaN     NaN     NaN
2020-12-13  12151.000000    12231.750000    9508.000000     9590.150391

我用来加载数据的代码是:

nifty = pd.read_csv('../Nifty.csv', index_col='Date',parse_dates=True)
nifty.drop(['Volume','Adj Close'],axis=1,inplace=True)
imputer = SimpleImputer(strategy = 'mean')
nifty_tr = pd.DataFrame(imputer.fit_transform(nifty), columns=nifty.columns, index=nifty.index)

我似乎无法弄清楚导致这种情况发生的原因。非常感谢您的帮助。

0 个答案:

没有答案