Skip to content

BUG: Respect check_dtype in assert_series_equal #32757

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 16 commits into from
Mar 17, 2020
Merged
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ Other
- Bug in :meth:`DataFrame.to_records` incorrectly losing timezone information in timezone-aware ``datetime64`` columns (:issue:`32535`)
- Fixed :func:`pandas.testing.assert_series_equal` to correctly raise if left object is a different subclass with ``check_series_type=True`` (:issue:`32670`).
- :meth:`IntegerArray.astype` now supports ``datetime64`` dtype (:issue:32538`)
- Fixed bug in :func:`pandas.testing.assert_series_equal` where dtypes were checked for ``Interval`` and ``ExtensionArray`` operands when ``check_dtype`` was ``False`` (:issue:`32747`)

.. ---------------------------------------------------------------------------

Expand Down
4 changes: 2 additions & 2 deletions pandas/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ def assert_series_equal(
f"is not equal to {right._values}."
)
raise AssertionError(msg)
elif is_interval_dtype(left.dtype) or is_interval_dtype(right.dtype):
elif is_interval_dtype(left.dtype) and is_interval_dtype(right.dtype):
assert_interval_array_equal(left.array, right.array)
elif is_categorical_dtype(left.dtype) or is_categorical_dtype(right.dtype):
_testing.assert_almost_equal(
Expand All @@ -1170,7 +1170,7 @@ def assert_series_equal(
check_dtype=check_dtype,
obj=str(obj),
)
elif is_extension_array_dtype(left.dtype) or is_extension_array_dtype(right.dtype):
elif is_extension_array_dtype(left.dtype) and is_extension_array_dtype(right.dtype):
assert_extension_array_equal(left._values, right._values)
elif needs_i8_conversion(left.dtype) or needs_i8_conversion(right.dtype):
# DatetimeArray or TimedeltaArray
Expand Down
39 changes: 39 additions & 0 deletions pandas/tests/util/test_assert_frame_equal.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

import pandas as pd
from pandas import DataFrame
import pandas._testing as tm

Expand Down Expand Up @@ -218,3 +219,41 @@ def test_frame_equal_unicode(df1, df2, msg, by_blocks_fixture, obj_fixture):
msg = msg.format(obj=obj_fixture)
with pytest.raises(AssertionError, match=msg):
tm.assert_frame_equal(df1, df2, by_blocks=by_blocks_fixture, obj=obj_fixture)


def test_assert_frame_equal_extension_dtype_mismatch():
# https://github.com/pandas-dev/pandas/issues/32747
left = DataFrame({"a": [1, 2, 3]}, dtype="Int64")
right = left.astype(int)

msg = (
"Attributes of DataFrame\\.iloc\\[:, 0\\] "
'\\(column name="a"\\) are different\n\n'
'Attribute "dtype" are different\n'
"\\[left\\]: Int64\n"
"\\[right\\]: int[32|64]"
)

tm.assert_frame_equal(left, right, check_dtype=False)

with pytest.raises(AssertionError, match=msg):
tm.assert_frame_equal(left, right, check_dtype=True)


def test_assert_frame_equal_interval_dtype_mismatch():
# https://github.com/pandas-dev/pandas/issues/32747
left = DataFrame({"a": [pd.Interval(0, 1)]}, dtype="interval")
right = left.astype(object)

msg = (
"Attributes of DataFrame\\.iloc\\[:, 0\\] "
'\\(column name="a"\\) are different\n\n'
'Attribute "dtype" are different\n'
"\\[left\\]: interval\\[int64\\]\n"
"\\[right\\]: object"
)

tm.assert_frame_equal(left, right, check_dtype=False)

with pytest.raises(AssertionError, match=msg):
tm.assert_frame_equal(left, right, check_dtype=True)
35 changes: 35 additions & 0 deletions pandas/tests/util/test_assert_series_equal.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

import pandas as pd
from pandas import Categorical, DataFrame, Series
import pandas._testing as tm

Expand Down Expand Up @@ -196,6 +197,40 @@ def test_series_equal_categorical_mismatch(check_categorical):
_assert_series_equal_both(s1, s2, check_categorical=check_categorical)


def test_assert_series_equal_extension_dtype_mismatch():
# https://github.com/pandas-dev/pandas/issues/32747
left = Series(pd.array([1, 2, 3], dtype="Int64"))
right = left.astype(int)

msg = """Attributes of Series are different

Attribute "dtype" are different
\\[left\\]: Int64
\\[right\\]: int[32|64]"""

tm.assert_series_equal(left, right, check_dtype=False)

with pytest.raises(AssertionError, match=msg):
tm.assert_series_equal(left, right, check_dtype=True)


def test_assert_series_equal_interval_dtype_mismatch():
# https://github.com/pandas-dev/pandas/issues/32747
left = Series([pd.Interval(0, 1)], dtype="interval")
right = left.astype(object)

msg = """Attributes of Series are different

Attribute "dtype" are different
\\[left\\]: interval\\[int64\\]
\\[right\\]: object"""

tm.assert_series_equal(left, right, check_dtype=False)

with pytest.raises(AssertionError, match=msg):
tm.assert_series_equal(left, right, check_dtype=True)


def test_series_equal_series_type():
class MySeries(Series):
pass
Expand Down