gpt4 book ai didi

pandas - dtype=datetime64[ns, UTC] 和时间戳之间的无效比较

转载 作者:行者123 更新时间:2023-12-05 01:28:30 25 4
gpt4 key购买 nike

这是数据集Dataset的一部分

  1. Ordered Quantity Time Stamp Medicine Name Actual Price
    14162 3 2020-10-17 10:06:33.790000+00:00 27699 263.82
    15747 2 2020-10-28 09:14:46.690000+00:00 27699 175.88
    24866 2 2020-12-27 14:16:26.259000+00:00 27699 175.88
    25962 2 2021-01-03 18:35:04.337000+00:00 27699 175.88
    29188 2 2021-01-28 08:17:40.975000+00:00 27699 175.88
    29565 2 2021-02-02 04:15:58.496000+00:00 27699 175.88
    30863 2 2021-02-09 07:19:44.897000+00:00 27699 175.88
    33931 2 2021-03-02 03:39:52.087000+00:00 27699 175.88
    35537 2 2021-03-13 15:19:58.131000+00:00 27699 175.88
    37429 1 2021-03-27 05:15:31.772000+00:00 27699 87.94
    47099 1 2021-05-03 07:16:10.483000+00:00 27699 87.94
    49135 2 2021-05-09 08:03:35.300000+00:00 27699 175.88
    66057 1 2021-07-07 07:50:21.414000+00:00 27699 87.94
    67937 1 2021-07-13 06:44:20.875000+00:00 27699 87.94

`

  import pandas as pd
data = pd.read_csv("data.csv")
data["Time Stamp"] = pd.to_datetime(data["Time Stamp"])
t1 = pd.Timestamp("2020-06-01 00:00:00.054000+00:00")
t2 = t1.date() + pd.DateOffset(months = 6)
data[(data["Time Stamp"] > t1) & (data["Time Stamp"] < t2)]
`

TypeError: dtype=datetime64[ns, UTC] 和时间戳之间的比较无效

为什么会出现此错误?

最佳答案

尝试:

在比较中不要使用 t2,而是使用 t2.tz_localize('utc'):

data[(data["Time Stamp"] > t1) & (data["Time Stamp"] < t2.tz_localize('utc'))]

使用normalize() 方法代替date() 方法:

t2=t1.normalize() + pd.DateOffset(months = 6)

关于pandas - dtype=datetime64[ns, UTC] 和时间戳之间的无效比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68506942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com