Skip to content

Commit 18fda97

Browse files
authored
Backport PR pandas-dev#52458: BUG: tm.assert_series_equalcheck_dtype=False) with different resos (pandas-dev#52526)
1 parent 8a89f2b commit 18fda97

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

doc/source/whatsnew/v2.0.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Bug fixes
2424
- Bug in :func:`to_datetime` and :func:`to_timedelta` when trying to convert numeric data with a :class:`ArrowDtype` (:issue:`52425`)
2525
- Bug in :meth:`Series.describe` not returning :class:`ArrowDtype` with ``pyarrow.float64`` type with numeric data (:issue:`52427`)
2626
- Fixed segfault in :meth:`Series.to_numpy` with ``null[pyarrow]`` dtype (:issue:`52443`)
27+
- Bug in :func:`pandas.testing.assert_series_equal` where ``check_dtype=False`` would still raise for datetime or timedelta types with different resolutions (:issue:`52449`)
2728

2829
.. ---------------------------------------------------------------------------
2930
.. _whatsnew_201.other:

pandas/_testing/asserters.py

+20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import operator
34
from typing import (
45
Literal,
56
cast,
@@ -10,6 +11,7 @@
1011
from pandas._libs.missing import is_matching_na
1112
from pandas._libs.sparse import SparseIndex
1213
import pandas._libs.testing as _testing
14+
from pandas._libs.tslibs.np_datetime import compare_mismatched_resolutions
1315

1416
from pandas.core.dtypes.common import (
1517
is_bool,
@@ -23,6 +25,7 @@
2325
)
2426
from pandas.core.dtypes.dtypes import (
2527
CategoricalDtype,
28+
DatetimeTZDtype,
2629
PandasDtype,
2730
)
2831
from pandas.core.dtypes.missing import array_equivalent
@@ -736,6 +739,23 @@ def assert_extension_array_equal(
736739
and isinstance(right, DatetimeLikeArrayMixin)
737740
and type(right) == type(left)
738741
):
742+
# GH 52449
743+
if not check_dtype and left.dtype.kind in "mM":
744+
if not isinstance(left.dtype, np.dtype):
745+
l_unit = cast(DatetimeTZDtype, left.dtype).unit
746+
else:
747+
l_unit = np.datetime_data(left.dtype)[0]
748+
if not isinstance(right.dtype, np.dtype):
749+
r_unit = cast(DatetimeTZDtype, left.dtype).unit
750+
else:
751+
r_unit = np.datetime_data(right.dtype)[0]
752+
if (
753+
l_unit != r_unit
754+
and compare_mismatched_resolutions(
755+
left._ndarray, right._ndarray, operator.eq
756+
).all()
757+
):
758+
return
739759
# Avoid slow object-dtype comparisons
740760
# np.asarray for case where we have a np.MaskedArray
741761
assert_numpy_array_equal(

pandas/tests/util/test_assert_series_equal.py

+18
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,21 @@ def test_identical_nested_series_is_equal():
404404
tm.assert_series_equal(x, x, check_exact=True)
405405
tm.assert_series_equal(x, y)
406406
tm.assert_series_equal(x, y, check_exact=True)
407+
408+
409+
@pytest.mark.parametrize("dtype", ["datetime64", "timedelta64"])
410+
def test_check_dtype_false_different_reso(dtype):
411+
# GH 52449
412+
ser_s = Series([1000213, 2131232, 21312331]).astype(f"{dtype}[s]")
413+
ser_ms = ser_s.astype(f"{dtype}[ms]")
414+
with pytest.raises(AssertionError, match="Attributes of Series are different"):
415+
tm.assert_series_equal(ser_s, ser_ms)
416+
tm.assert_series_equal(ser_ms, ser_s, check_dtype=False)
417+
418+
ser_ms -= Series([1, 1, 1]).astype(f"{dtype}[ms]")
419+
420+
with pytest.raises(AssertionError, match="Series are different"):
421+
tm.assert_series_equal(ser_s, ser_ms)
422+
423+
with pytest.raises(AssertionError, match="Series are different"):
424+
tm.assert_series_equal(ser_s, ser_ms, check_dtype=False)

0 commit comments

Comments
 (0)