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
4 changes: 2 additions & 2 deletions pandas/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,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 @@ -1169,7 +1169,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
37 changes: 37 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 @@ -194,3 +195,39 @@ def test_series_equal_categorical_mismatch(check_categorical):
tm.assert_series_equal(s1, s2, check_categorical=check_categorical)
else:
_assert_series_equal_both(s1, s2, check_categorical=check_categorical)


@pytest.mark.parametrize("check_dtype", [True, False])
def test_assert_series_equal_extension_dtype_mismatch(check_dtype):
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\\]: int64"""

if check_dtype:
with pytest.raises(AssertionError, match=msg):
tm.assert_series_equal(left, right, check_dtype=check_dtype)
else:
tm.assert_series_equal(left, right, check_dtype=check_dtype)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you just test both True / False cases inline here without the parametrization? (it actually gets you rid of the if/else, while for the rest not adding any code)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(and same below)



@pytest.mark.parametrize("check_dtype", [True, False])
def test_assert_series_equal_interval_dtype_mismatch(check_dtype):
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"""

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