0

我正在从polygon.io提取数据,它以 Unix Msec 时间戳的形式返回时间,如下所示,之后我无法将其转换为期望的 mplfinance 可用的索引TypeError: Expect data.index as DatetimeIndex

数据

我有以下代码,其中from_unixtime有尚未定义的占位符函数:

import mplfinance as mpf
import pandas as pd
from polygon import RESTClient

def main():
    key = "keyhere"

    with RESTClient(key) as client:
        start = "2019-01-01"
        end = "2019-02-01"
        resp = client.stocks_equities_aggregates("AAPL", 1, "minute", start, end, unadjusted=False)
        df = pd.DataFrame(resp.results)
        df.index = [from_unixtime(ts) for ts in df['t']]
        df.index.name = 'Timestamp'
      
        # mpf expects a dataframe containing Open, High, Low, and Close data with a Pandas TimetimeIndex
        df.columns = ['Volume', 'Volume Weighted', 'Open', 'Close', 'High', 'Low', 'Time', 'Num Items']
        mpf.plot(df, type='candlestick', no_xgaps = True)

if __name__ == '__main__':
    main()
4

1 回答 1

1

尝试更换

df.index = [from_unixtime(ts) for ts in df['t']]

df.index = pd.DatetimeIndex( pd.to_datetime(df['t'],unit='s') )

lmk

于 2021-12-24T20:46:14.083 回答