Anuvicleo
Anuvicleo

Reputation: 107

Invalid comparison between dtype=datetime64[ns, UTC] and Timestamp

This is some part of datasetDataset

  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: Invalid comparison between dtype=datetime64[ns, UTC] and Timestamp

Why am I getting this error?

Upvotes: 3

Views: 11266

Answers (1)

Anurag Dabas
Anurag Dabas

Reputation: 24314

try:

Instead of using t2 in comparision use t2.tz_localize('utc'):

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

OR

use normalize() method instead of date() method:

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

Upvotes: 8

Related Questions