[英]Pandas dataframe how to get data from one time frame to another 1 min time frame in Time series data
How to change the Timeseries data from one time period to 1 min time periods for below data 如何将以下数据的时间序列数据从一个时间段更改为1分钟时间段
Time Series Data: 时间序列数据:
Open High Low Close
DateTime
2019-03-22 09:15:00 1342 1342 1342 1342
2019-03-22 09:15:09 1344 1344 1344 1344
2019-03-22 09:15:12 1344.4 1344.4 1344.4 1344.4
2019-03-22 09:15:17 1345 1345 1345 1345
2019-03-22 09:15:22 1344.4 1345.4 1344.4 1344.4
2019-03-22 09:15:24 1349 1349 1349 1349
2019-03-22 09:15:32 1346 1346 1346 1346
2019-03-22 09:15:36 1346 1346 1346 1346
2019-03-22 09:15:41 1346.25 1346.25 1346.25 1346.25
2019-03-22 09:15:43 1346.25 1346.25 1346.25 1346.25
2019-03-22 09:15:45 1346 1346 1346 1346
2019-03-22 09:15:55 1344.45 1344.45 1344.45 1344.45
2019-03-22 09:16:00 1344.4 1344.4 1344.4 1344.4
I would like to have as 1min time frame data. 我希望有1分钟的时间范围数据。 really confused with resample function, to_period...etc.
确实与重采样功能,to_period ...等混淆。
If you want to have correct OHLC values after the resampling, you need to apply proper aggregation functions (taking first
for Open, max
for High, min
for Low and last
for Close): 如果要在重新采样后获得正确的OHLC值,则需要应用适当的聚合函数(
first
对Open进行取值,对High取max
,对Low取min
, last
对Close取值):
df.resample('1T').agg({
'Open': 'first',
'High': 'max',
'Low': 'min',
'Close': 'last'})
Output: 输出:
Open High Low Close
DateTime
2019-03-22 09:15:00 1342.0 1349.0 1342.0 1344.45
2019-03-22 09:16:00 1344.4 1344.4 1344.4 1344.40
Resample returns a Resampler object on which you apply the aggregate function, Resample返回一个Resampler对象,在该对象上应用了聚合函数,
df.resample('1T').last()
Open High Low Close
DateTime
2019-03-22 09:15:00 1344.45 1344.45 1344.45 1344.45
2019-03-22 09:16:00 1344.40 1344.40 1344.40 1344.40
If you only wish to change the period but not aggregate the values, use to_period 如果仅希望更改时间段而不希望汇总值,请使用to_period
df.to_period('1T')
Open High Low Close
DateTime
2019-03-22 09:15 1342.00 1342.00 1342.00 1342.00
2019-03-22 09:15 1344.00 1344.00 1344.00 1344.00
2019-03-22 09:15 1344.40 1344.40 1344.40 1344.40
2019-03-22 09:15 1345.00 1345.00 1345.00 1345.00
2019-03-22 09:15 1344.40 1345.40 1344.40 1344.40
2019-03-22 09:15 1349.00 1349.00 1349.00 1349.00
2019-03-22 09:15 1346.00 1346.00 1346.00 1346.00
2019-03-22 09:15 1346.00 1346.00 1346.00 1346.00
2019-03-22 09:15 1346.25 1346.25 1346.25 1346.25
2019-03-22 09:15 1346.25 1346.25 1346.25 1346.25
2019-03-22 09:15 1346.00 1346.00 1346.00 1346.00
2019-03-22 09:15 1344.45 1344.45 1344.45 1344.45
2019-03-22 09:16 1344.40 1344.40 1344.40 1344.40
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.