大佬求助,dateframe(df)里面的“SLO”这一列为时间序列, 格式如2021-04-16T20:30:53.000+00:00
我想实现这一列与当前时间(同样也是UTC时区)比较。现在代码如下,但是我发现pd.to_datetime运行后,“SLO”那一列仍然是timestamp类型,而UTC是datetime类型。 如果我继续运行到df_miss = df[df["SLO"] -utc_now < 0],则会输出error "TypeError: Timestamp subtraction must have the same timezones or no timezones" . 求教大神问题到底出在哪里
#将字符串格式转换成timestamp格式
df["SLO"] = pd.to_datetime(df["SLO"],format = "%Y-%m-%dT%H:%M:%S")
print(df["SLO"].dtypes)
print(type(df["SLO"][0]))
print(df["SLO"][0])
#将time stamp转换成datetime 格式方便计算时间差
df.loc[:,"SLO"] = pd.to_datetime(df.loc[:,"SLO"])
print(df["SLO"].dtypes)
print(type(df["SLO"][0]))
print(df["SLO"][0])
utc_now = datetime.datetime.utcnow().astimezone(timezone.utc)
print(utc_now)
print(type(utc_now))
df_miss = df[df["SLO"] -utc_now < 0]
print结果如下:
datetime64[ns, UTC] <class 'pandas._libs.tslibs.timestamps.Timestamp'> 2021-04-16 21:38:02+00:00 datetime64[ns, UTC] <class 'pandas._libs.tslibs.timestamps.Timestamp'> 2021-04-16 21:38:02+00:00 2021-04-22 22:43:03.755049+00:00 <class 'datetime.datetime'>
运行df_miss = df[df["SLO"] -utc_now < 0] 之后会产生error :
TypeError: Timestamp subtraction must have the same timezones or no timezones