Qiang Yao
Qiang Yao

Reputation: 185

How to compare two Series of Datetime by ignoring the datetime format

I want to compare two Series containing datetimes. One Series has ISO datetime format

[left]:  [2010-01-01T00:00:00.000Z]

The other one is not

[right]: [2010-01-01 00:00:00]

I am using the pandas assert pd.testing.assert_series_equal(left, right) Are there any easy ways to do it without explicitly converting them to the same format?

Upvotes: 1

Views: 572

Answers (1)

aaossa
aaossa

Reputation: 3852

The format on your left series includes a timezone, but the right series does not. You could transform both series from a string to a datetime format using pandas.to_datetime:

left = pd.Series(["2010-01-01T00:00:00.000Z"])
right = pd.Series(["2010-01-01 00:00:00"])

left = pd.to_datetime(left, utc=True)
right = pd.to_datetime(right, utc=True)

The utc=True part is important. From the docs:

If True, the function always returns a timezone-aware UTC-localized Timestamp, Series or DatetimeIndex. To do this, timezone-naive inputs are localized as UTC, while timezone-aware inputs are converted to UTC.

Now, assuming that your series have the same index, you could directly compare them this way:

(left == right).all()  # `.all()` verifies if a series only contains True values

Upvotes: 1

Related Questions