繁体   English   中英

python pandas - 无法重新采样时间序列数据

[英]python pandas - unable to resample timeseries data

我有看起来像这样的时间序列数据:

                  ticker        close
created_at                                   
2020-06-10 18:30:00+00:00   TSLA  1017.419312
2020-06-10 17:02:00+00:00   TSLA  1014.354980
2020-06-10 17:03:00+00:00   TSLA  1014.922302
2020-06-10 17:04:00+00:00   TSLA  1015.626709
2020-06-10 17:05:00+00:00   TSLA  1016.400024
2020-06-10 17:06:00+00:00   TSLA  1017.223389
2020-06-10 17:07:00+00:00   TSLA  1016.110107
2020-06-10 17:08:00+00:00   TSLA  1016.109985
  ..........................................

我正在尝试使用 5 分钟间隔重新采样这是我的代码:

df = pd.read_sql_query("SELECT created_at,ticker,close FROM market_data_history WHERE ticker='TSLA' and CREATED_AT > '2020-06-10 00:01:00+00' AND created_at < '2020-06-11 00:01:00+00'",index_col='created_at',con=engine)
# df = pd.read_csv("market_data_history.csv", usecols = ['created_at','ticker','close','volume'])
print(df)


d=df.resample('5T')
print(d)

然而,output 只是显示

DatetimeIndexResampler [freq=<5 * Minutes>, axis=0, closed=left, label=left, convention=start, base=0]

不知道为什么它没有得到应用,有人可以帮忙吗

resample可以看成是一种group-by function。 您需要指定如何聚合重新采样的数据。

例如:

df.resample("5T").max()

会产生:

                          ticker        close
created_at                                   
2020-06-10 17:00:00+00:00   TSLA  1015.626709
2020-06-10 17:05:00+00:00   TSLA  1017.223389
2020-06-10 17:10:00+00:00    NaN          NaN
2020-06-10 17:15:00+00:00    NaN          NaN
2020-06-10 17:20:00+00:00    NaN          NaN
2020-06-10 17:25:00+00:00    NaN          NaN
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM