![](/img/trans.png)
[英]How to transform recurrent time series pandas data frame to pandas multi-index data frame
[英]Pandas dataframe how to get data from one time frame to another 1 min time frame in Time series data
如何將以下數據的時間序列數據從一個時間段更改為1分鍾時間段
時間序列數據:
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
我希望有1分鍾的時間范圍數據。 確實與重采樣功能,to_period ...等混淆。
如果要在重新采樣后獲得正確的OHLC值,則需要應用適當的聚合函數( first
對Open進行取值,對High取max
,對Low取min
, last
對Close取值):
df.resample('1T').agg({
'Open': 'first',
'High': 'max',
'Low': 'min',
'Close': 'last'})
輸出:
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返回一個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
如果僅希望更改時間段而不希望匯總值,請使用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.