Skip to content

Commit b637ad2

Browse files
authored
BUG: tm.assert_series_equalcheck_dtype=False) with different resos (#52458)
1 parent 4adc5fa commit b637ad2

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

doc/source/whatsnew/v2.0.1.rst

Lines changed: 1 addition & 0 deletions
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

Lines changed: 20 additions & 0 deletions
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
TYPE_CHECKING,
56
Literal,
@@ -11,6 +12,7 @@
1112
from pandas._libs.missing import is_matching_na
1213
from pandas._libs.sparse import SparseIndex
1314
import pandas._libs.testing as _testing
15+
from pandas._libs.tslibs.np_datetime import compare_mismatched_resolutions
1416

1517
from pandas.core.dtypes.common import (
1618
is_bool,
@@ -22,6 +24,7 @@
2224
)
2325
from pandas.core.dtypes.dtypes import (
2426
CategoricalDtype,
27+
DatetimeTZDtype,
2528
ExtensionDtype,
2629
PandasDtype,
2730
)
@@ -744,6 +747,23 @@ def assert_extension_array_equal(
744747
and isinstance(right, DatetimeLikeArrayMixin)
745748
and type(right) == type(left)
746749
):
750+
# GH 52449
751+
if not check_dtype and left.dtype.kind in "mM":
752+
if not isinstance(left.dtype, np.dtype):
753+
l_unit = cast(DatetimeTZDtype, left.dtype).unit
754+
else:
755+
l_unit = np.datetime_data(left.dtype)[0]
756+
if not isinstance(right.dtype, np.dtype):
757+
r_unit = cast(DatetimeTZDtype, left.dtype).unit
758+
else:
759+
r_unit = np.datetime_data(right.dtype)[0]
760+
if (
761+
l_unit != r_unit
762+
and compare_mismatched_resolutions(
763+
left._ndarray, right._ndarray, operator.eq
764+
).all()
765+
):
766+
return
747767
# Avoid slow object-dtype comparisons
748768
# np.asarray for case where we have a np.MaskedArray
749769
assert_numpy_array_equal(

pandas/tests/util/test_assert_series_equal.py

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

0 commit comments

Comments
 (0)