Skip to content

Commit

Permalink
Backport PR #52458: BUG: tm.assert_series_equalcheck_dtype=False) wit…
Browse files Browse the repository at this point in the history
…h different resos (#52526)
  • Loading branch information
mroeschke committed Apr 7, 2023
1 parent 8a89f2b commit 18fda97
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Bug fixes
- Bug in :func:`to_datetime` and :func:`to_timedelta` when trying to convert numeric data with a :class:`ArrowDtype` (:issue:`52425`)
- Bug in :meth:`Series.describe` not returning :class:`ArrowDtype` with ``pyarrow.float64`` type with numeric data (:issue:`52427`)
- Fixed segfault in :meth:`Series.to_numpy` with ``null[pyarrow]`` dtype (:issue:`52443`)
- 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`)

.. ---------------------------------------------------------------------------
.. _whatsnew_201.other:
Expand Down
20 changes: 20 additions & 0 deletions pandas/_testing/asserters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import operator
from typing import (
Literal,
cast,
Expand All @@ -10,6 +11,7 @@
from pandas._libs.missing import is_matching_na
from pandas._libs.sparse import SparseIndex
import pandas._libs.testing as _testing
from pandas._libs.tslibs.np_datetime import compare_mismatched_resolutions

from pandas.core.dtypes.common import (
is_bool,
Expand All @@ -23,6 +25,7 @@
)
from pandas.core.dtypes.dtypes import (
CategoricalDtype,
DatetimeTZDtype,
PandasDtype,
)
from pandas.core.dtypes.missing import array_equivalent
Expand Down Expand Up @@ -736,6 +739,23 @@ def assert_extension_array_equal(
and isinstance(right, DatetimeLikeArrayMixin)
and type(right) == type(left)
):
# GH 52449
if not check_dtype and left.dtype.kind in "mM":
if not isinstance(left.dtype, np.dtype):
l_unit = cast(DatetimeTZDtype, left.dtype).unit
else:
l_unit = np.datetime_data(left.dtype)[0]
if not isinstance(right.dtype, np.dtype):
r_unit = cast(DatetimeTZDtype, left.dtype).unit
else:
r_unit = np.datetime_data(right.dtype)[0]
if (
l_unit != r_unit
and compare_mismatched_resolutions(
left._ndarray, right._ndarray, operator.eq
).all()
):
return
# Avoid slow object-dtype comparisons
# np.asarray for case where we have a np.MaskedArray
assert_numpy_array_equal(
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/util/test_assert_series_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,21 @@ def test_identical_nested_series_is_equal():
tm.assert_series_equal(x, x, check_exact=True)
tm.assert_series_equal(x, y)
tm.assert_series_equal(x, y, check_exact=True)


@pytest.mark.parametrize("dtype", ["datetime64", "timedelta64"])
def test_check_dtype_false_different_reso(dtype):
# GH 52449
ser_s = Series([1000213, 2131232, 21312331]).astype(f"{dtype}[s]")
ser_ms = ser_s.astype(f"{dtype}[ms]")
with pytest.raises(AssertionError, match="Attributes of Series are different"):
tm.assert_series_equal(ser_s, ser_ms)
tm.assert_series_equal(ser_ms, ser_s, check_dtype=False)

ser_ms -= Series([1, 1, 1]).astype(f"{dtype}[ms]")

with pytest.raises(AssertionError, match="Series are different"):
tm.assert_series_equal(ser_s, ser_ms)

with pytest.raises(AssertionError, match="Series are different"):
tm.assert_series_equal(ser_s, ser_ms, check_dtype=False)

0 comments on commit 18fda97

Please sign in to comment.