Skip to content

Commit 9fe01dd

Browse files
Backport PR pandas-dev#37270 on branch 1.1.x: REGR: Make comparisons consistent for PeriodDtype (pandas-dev#37307)
Co-authored-by: Daniel Saxton <[email protected]>
1 parent 79d7caa commit 9fe01dd

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

doc/source/whatsnew/v1.1.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Fixed regressions
2020
- Fixed regression in :class:`RollingGroupby` with ``sort=False`` not being respected (:issue:`36889`)
2121
- Fixed regression in :meth:`Series.astype` converting ``None`` to ``"nan"`` when casting to string (:issue:`36904`)
2222
- Fixed regression in :class:`RollingGroupby` causing a segmentation fault with Index of dtype object (:issue:`36727`)
23+
- Fixed regression in :class:`PeriodDtype` comparing both equal and unequal to its string representation (:issue:`37265`)
2324

2425
.. ---------------------------------------------------------------------------
2526

pandas/core/dtypes/dtypes.py

+3
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,9 @@ def __eq__(self, other: Any) -> bool:
894894

895895
return isinstance(other, PeriodDtype) and self.freq == other.freq
896896

897+
def __ne__(self, other: Any) -> bool:
898+
return not self.__eq__(other)
899+
897900
def __setstate__(self, state):
898901
# for pickle compat. __getstate__ is defined in the
899902
# PandasExtensionDtype superclass and uses the public properties to

pandas/tests/dtypes/test_dtypes.py

+7
Original file line numberDiff line numberDiff line change
@@ -991,3 +991,10 @@ def test_is_dtype_no_warning(check):
991991

992992
with tm.assert_produces_warning(None):
993993
check(data["A"])
994+
995+
996+
def test_period_dtype_compare_to_string():
997+
# https://github.com/pandas-dev/pandas/issues/37265
998+
dtype = PeriodDtype(freq="M")
999+
assert (dtype == "period[M]") is True
1000+
assert (dtype != "period[M]") is False

0 commit comments

Comments
 (0)