我从polygon.io中提取数据,它以Unix时间戳的形式返回时间,在此之后,我很难将其转换为期望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()
发布于 2021-12-24 20:46:14
试着替换
df.index = [from_unixtime(ts) for ts in df['t']]
使用
df.index = pd.DatetimeIndex( pd.to_datetime(df['t'],unit='s') )
lmk
https://stackoverflow.com/questions/70474556
复制相似问题