
[英]Dataframe Index dtype is dtype='datetime64[ns], plot shows index year as a strange #
[英]Converting a dataframe's datetime64[ns] index to a comparable datetime dtype
尝试为我的 dataframe 创建掩码,但无法将上限/下限日期时间与 dataframe 的索引进行比较,因为它是 datetime64[ns]。 我已经看到解决方案是通过 pd.Timestamp 进行转换 - 但是我仍然收到值错误。
此外,我尝试转换索引并抛出错误:“无法将输入...系列...转换为时间戳”
输入:
x = yf.Ticker('^GSPC').history(period='max',interval='1d').loc[:,['Open']]
stdate = pd.Timestamp(2015,12,31)
edate = dt.datetime.today()
y = x.index > stdate
实际 OUTPUT:
*"Invalid comparison between dtype=datetime64[ns, TIMEZONE] and Timestamp"*
预计 OUTPUT:
[FALSE, FALSE, FALSE, TRUE, TRUE... TRUE]
Datetime64 索引可以细化为只有日期 by.date
df.index.date >= date or df.index.datetime >= datetime
会工作
使用 numpy.datetime64
注意:似乎无法比较时间感知日期时间,使用tz_localize
删除它的时区
或者您可以将日期时间转换为时间戳(int)
import numpy as np
import pandas as pd
s = pd.Series([0, 1669345200 * 10**9], dtype="datetime64[ns]").dt.tz_localize("UTC")
print(s.info())
stdate = np.datetime64("2015-12-31T00:00:00")
edate = np.datetime64("now")
print(stdate)
print(edate)
print(s.dt.tz_localize(None) > stdate)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.