diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 8fe2b3c60d6d0..be43e3a6c856b 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -798,6 +798,8 @@ def __hash__(self) -> int: def __eq__(self, other: Any) -> bool: if isinstance(other, str): + if other.startswith("M8["): + other = "datetime64[" + other[3:] return other == self.name return ( diff --git a/pandas/tests/dtypes/test_common.py b/pandas/tests/dtypes/test_common.py index b9c8f3a8dd494..1708139a397ab 100644 --- a/pandas/tests/dtypes/test_common.py +++ b/pandas/tests/dtypes/test_common.py @@ -75,6 +75,10 @@ def test_numpy_string_dtype(self): "datetime64[ns, US/Eastern]", "datetime64[ns, Asia/Tokyo]", "datetime64[ns, UTC]", + # GH#33885 check that the M8 alias is understood + "M8[ns, US/Eastern]", + "M8[ns, Asia/Tokyo]", + "M8[ns, UTC]", ], ) def test_datetimetz_dtype(self, dtype): diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index 519f2f3eead1c..b55ff92e3f6a4 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -278,12 +278,14 @@ def test_is_dtype(self, dtype): assert not DatetimeTZDtype.is_dtype(None) assert DatetimeTZDtype.is_dtype(dtype) assert DatetimeTZDtype.is_dtype("datetime64[ns, US/Eastern]") + assert DatetimeTZDtype.is_dtype("M8[ns, US/Eastern]") assert not DatetimeTZDtype.is_dtype("foo") assert DatetimeTZDtype.is_dtype(DatetimeTZDtype("ns", "US/Pacific")) assert not DatetimeTZDtype.is_dtype(np.float64) def test_equality(self, dtype): assert is_dtype_equal(dtype, "datetime64[ns, US/Eastern]") + assert is_dtype_equal(dtype, "M8[ns, US/Eastern]") assert is_dtype_equal(dtype, DatetimeTZDtype("ns", "US/Eastern")) assert not is_dtype_equal(dtype, "foo") assert not is_dtype_equal(dtype, DatetimeTZDtype("ns", "CET")) @@ -294,6 +296,8 @@ def test_equality(self, dtype): # numpy compat assert is_dtype_equal(np.dtype("M8[ns]"), "datetime64[ns]") + assert dtype == "M8[ns, US/Eastern]" + def test_basic(self, dtype): assert is_datetime64tz_dtype(dtype)