【问题标题】:Unix ms timestamp pandas index (plotting polygon.io data with mplfinance)Unix ms 时间戳 pandas 索引(使用 mplfinance 绘制 polygon.io 数据)
【发布时间】:2022-01-25 04:24:03
【问题描述】:

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

我有以下代码,其中有我尚未定义的占位符函数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()

【问题讨论】:

    标签: python pandas dataframe mplfinance


    【解决方案1】:

    尝试替换

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

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

    lmk

    【讨论】:

    • 非常感谢丹尼尔,由于数据以毫秒为单位,我必须除以 1000(单位为秒,不支持毫秒),我认为这可行! df.index = pd.DatetimeIndex( pd.to_datetime(df['t']/1000,unit='s') )
    猜你喜欢
    • 2013-05-07
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 2020-11-03
    • 2018-10-29
    • 2023-04-05
    • 1970-01-01
    • 2013-12-03
    相关资源
    最近更新 更多