Unix ms 时间戳 pandas 索引(用 mplfinance 绘制 polygon.io 数据)
Unix ms timestamp pandas index (plotting polygon.io data with mplfinance)
我正在从 polygon.io 中提取数据,它 returns 时间作为 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()
尝试替换
df.index = [from_unixtime(ts) for ts in df['t']]
和
df.index = pd.DatetimeIndex( pd.to_datetime(df['t'],unit='s') )
lmk
我正在从 polygon.io 中提取数据,它 returns 时间作为 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()
尝试替换
df.index = [from_unixtime(ts) for ts in df['t']]
和
df.index = pd.DatetimeIndex( pd.to_datetime(df['t'],unit='s') )
lmk