Skip to content

ENH: Add index to output of assert_series_equal on category and datetime values #33575

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 1 commit into from
Apr 26, 2020
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
31 changes: 25 additions & 6 deletions pandas/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,12 @@ def _raise(left, right, err_msg):


def assert_extension_array_equal(
left, right, check_dtype=True, check_less_precise=False, check_exact=False
left,
right,
check_dtype=True,
check_less_precise=False,
check_exact=False,
index_values=None,
):
"""
Check that left and right ExtensionArrays are equal.
Expand All @@ -1016,6 +1021,8 @@ def assert_extension_array_equal(
If int, then specify the digits to compare.
check_exact : bool, default False
Whether to compare number exactly.
index_values : numpy.ndarray, default None
optional index (shared by both left and right), used in output.

Notes
-----
Expand All @@ -1031,24 +1038,31 @@ def assert_extension_array_equal(
if hasattr(left, "asi8") and type(right) == type(left):
# Avoid slow object-dtype comparisons
# np.asarray for case where we have a np.MaskedArray
assert_numpy_array_equal(np.asarray(left.asi8), np.asarray(right.asi8))
assert_numpy_array_equal(
np.asarray(left.asi8), np.asarray(right.asi8), index_values=index_values
)
return

left_na = np.asarray(left.isna())
right_na = np.asarray(right.isna())
assert_numpy_array_equal(left_na, right_na, obj="ExtensionArray NA mask")
assert_numpy_array_equal(
left_na, right_na, obj="ExtensionArray NA mask", index_values=index_values
)

left_valid = np.asarray(left[~left_na].astype(object))
right_valid = np.asarray(right[~right_na].astype(object))
if check_exact:
assert_numpy_array_equal(left_valid, right_valid, obj="ExtensionArray")
assert_numpy_array_equal(
left_valid, right_valid, obj="ExtensionArray", index_values=index_values
)
else:
_testing.assert_almost_equal(
left_valid,
right_valid,
check_dtype=check_dtype,
check_less_precise=check_less_precise,
obj="ExtensionArray",
index_values=index_values,
)


Expand Down Expand Up @@ -1181,12 +1195,17 @@ def assert_series_equal(
check_less_precise=check_less_precise,
check_dtype=check_dtype,
obj=str(obj),
index_values=np.asarray(left.index),
)
elif is_extension_array_dtype(left.dtype) and is_extension_array_dtype(right.dtype):
assert_extension_array_equal(left._values, right._values)
assert_extension_array_equal(
left._values, right._values, index_values=np.asarray(left.index)
Copy link
Member

Choose a reason for hiding this comment

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

Do you have to use asarray here or could you just pass back the DatetimeArray? Would make for nicer output if the date time values were shown

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is a good point which I hadn't thought of. However, it isn't quite as simple as that because we only know that the data is an extension array at this point - the index may not be. Also, my understanding of extension array is very poor, but I thought it could be used for things other than datetimes?

The main reason though, is that the data is currently dislayed as numeric timestamps, so it seems reasonable to fix the index display at the same time as the data. And that sounds like a seperate PR to me. I am looking at doing it (when I get time), but as mentioned above, I need to figure out extension arrays first.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And thanks for looking at the PR. Still not sure if I'm supposed to click 'Resolve conversation' now.

)
elif needs_i8_conversion(left.dtype) or needs_i8_conversion(right.dtype):
# DatetimeArray or TimedeltaArray
assert_extension_array_equal(left._values, right._values)
assert_extension_array_equal(
left._values, right._values, index_values=np.asarray(left.index)
)
else:
_testing.assert_almost_equal(
left._values,
Expand Down
34 changes: 33 additions & 1 deletion pandas/tests/util/test_assert_series_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def test_series_equal_length_mismatch(check_less_precise):
tm.assert_series_equal(s1, s2, check_less_precise=check_less_precise)


def test_series_equal_values_mismatch(check_less_precise):
def test_series_equal_numeric_values_mismatch(check_less_precise):
msg = """Series are different

Series values are different \\(33\\.33333 %\\)
Expand All @@ -180,6 +180,38 @@ def test_series_equal_values_mismatch(check_less_precise):
tm.assert_series_equal(s1, s2, check_less_precise=check_less_precise)


def test_series_equal_categorical_values_mismatch(check_less_precise):
msg = """Series are different

Series values are different \\(66\\.66667 %\\)
\\[index\\]: \\[0, 1, 2\\]
\\[left\\]: \\[a, b, c\\]
Categories \\(3, object\\): \\[a, b, c\\]
\\[right\\]: \\[a, c, b\\]
Categories \\(3, object\\): \\[a, b, c\\]"""

s1 = Series(Categorical(["a", "b", "c"]))
s2 = Series(Categorical(["a", "c", "b"]))

with pytest.raises(AssertionError, match=msg):
tm.assert_series_equal(s1, s2, check_less_precise=check_less_precise)


def test_series_equal_datetime_values_mismatch(check_less_precise):
msg = """numpy array are different

numpy array values are different \\(100.0 %\\)
\\[index\\]: \\[0, 1, 2\\]
\\[left\\]: \\[1514764800000000000, 1514851200000000000, 1514937600000000000\\]
\\[right\\]: \\[1549065600000000000, 1549152000000000000, 1549238400000000000\\]"""

s1 = Series(pd.date_range("2018-01-01", periods=3, freq="D"))
s2 = Series(pd.date_range("2019-02-02", periods=3, freq="D"))

with pytest.raises(AssertionError, match=msg):
tm.assert_series_equal(s1, s2, check_less_precise=check_less_precise)


def test_series_equal_categorical_mismatch(check_categorical):
msg = """Attributes of Series are different

Expand Down