Skip to content

BUG: tm.assert_series_equalcheck_dtype=False) with different resos #52458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -23,6 +23,7 @@ Bug fixes
- Fixed bug in :func:`merge` when merging with ``ArrowDtype`` one one and a NumPy dtype on the other side (:issue:`52406`)
- 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 (
TYPE_CHECKING,
Literal,
Expand All @@ -11,6 +12,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 @@ -22,6 +24,7 @@
)
from pandas.core.dtypes.dtypes import (
CategoricalDtype,
DatetimeTZDtype,
ExtensionDtype,
PandasDtype,
)
Expand Down Expand Up @@ -744,6 +747,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 @@ -405,3 +405,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)