第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用 python 重新采樣 olhc 數(shù)據(jù)幀時(shí)出錯(cuò)

使用 python 重新采樣 olhc 數(shù)據(jù)幀時(shí)出錯(cuò)

莫回?zé)o 2023-09-12 17:14:05
當(dāng)嘗試將 OHLC 數(shù)據(jù)幀從 1m 重新采樣到每小時(shí)時(shí),我收到此錯(cuò)誤:數(shù)據(jù)框df.info()#   Column     Dtype         ---  ------     -----          0   Date_Time  datetime64[ns] 1   Open       float64        2   High       float64        3   Low        float64        4   Close      float64df.tail()            Date_Time            Open    High    Low     Close    1692259 2014-12-30 20:51:00  2321.0  1213.0  1223.0  2334.0    1692260 2014-12-30 20:52:00  2342.0  2322.0  2332.0  2332.0    1692261 2014-12-30 20:53:00  3421.0  2322.0  2334.0  2123.0    1692262 2014-12-30 20:54:00  2312.0  2332.0  2324.0  2321.0    1692263 2014-12-30 20:55:00  2312.0  1212.0  2343.0  2323.0...嘗試1df_ohlc = df.resample('60T', on='Date_Time').ohlc()錯(cuò)誤DataError: No numeric types to aggregate嘗試2使用 venky__ 推薦另一篇具有類似解決方案的帖子df_ohlc = df.resample('60T', on='Date_Time').agg({    'Open':'first',    'High':'max',    'Low':'min',    'Close':'last'})包含 NaN,但 df 是干凈的。如何避免這種情況?                       Open    High     Low   CloseDate_Time                                          2015-12-26 18:00:00     NaN     NaN     NaN     NaN2015-12-26 19:00:00     NaN     NaN     NaN     NaN2015-12-26 20:00:00     NaN     NaN     NaN     NaN2015-12-26 21:00:00     NaN     NaN     NaN     NaN2015-12-26 22:00:00     NaN     NaN     NaN     NaN
查看完整描述

1 回答

?
隔江千里

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超10個(gè)贊

下一個(gè)版本的代碼對(duì)我有用,由于您提供的行數(shù)很少,我只進(jìn)行了 2 分鐘的聚合。

我認(rèn)為不起作用的原因是您的數(shù)據(jù)采用字符串格式,而不是需要日期時(shí)間列采用日期時(shí)間格式,其余部分采用浮點(diǎn)數(shù)格式,而不是字符串。我在代碼中正確地將字符串轉(zhuǎn)換為日期時(shí)間。

或者不工作的另一個(gè)原因是因?yàn)槟銢]有這樣做df.set_index('Date_Time'),我也在我的代碼中這樣做了。

在線嘗試一下!

import io, pandas as pd, numpy as np


df = pd.read_csv(io.StringIO("""

Date_Time,Open,High,Low,Close

2014-12-30 20:51:00,2321.0,1213.0,1223.0,2334.0

2014-12-30 20:52:00,2342.0,2322.0,2332.0,2332.0

2014-12-30 20:53:00,3421.0,2322.0,2334.0,2123.0

2014-12-30 20:54:00,2312.0,2332.0,2324.0,2321.0

2014-12-30 20:55:00,2312.0,1212.0,2343.0,2323.0

"""))


df.Date_Time = pd.to_datetime(df.Date_Time)

df.set_index('Date_Time')


print(df)


df = df.resample('2min', on = 'Date_Time').agg({

    'Open':'first',

    'High':'max',

    'Low':'min',

    'Close':'last',

})


print(df)

輸出是:


            Date_Time    Open    High     Low   Close

0 2014-12-30 20:51:00  2321.0  1213.0  1223.0  2334.0

1 2014-12-30 20:52:00  2342.0  2322.0  2332.0  2332.0

2 2014-12-30 20:53:00  3421.0  2322.0  2334.0  2123.0

3 2014-12-30 20:54:00  2312.0  2332.0  2324.0  2321.0

4 2014-12-30 20:55:00  2312.0  1212.0  2343.0  2323.0

                       Open    High     Low   Close

Date_Time

2014-12-30 20:50:00  2321.0  1213.0  1223.0  2334.0

2014-12-30 20:52:00  2342.0  2322.0  2332.0  2123.0

2014-12-30 20:54:00  2312.0  2332.0  2324.0  2323.0


查看完整回答
反對(duì) 回復(fù) 2023-09-12
  • 1 回答
  • 0 關(guān)注
  • 95 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)