Skip to content

BUG: assert_frame_equal exception for datetime #37609 #38157

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
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
21 changes: 20 additions & 1 deletion pandas/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1456,7 +1456,14 @@ def assert_series_equal(
check_dtype=check_dtype,
index_values=np.asarray(left.index),
)
elif needs_i8_conversion(left.dtype) or needs_i8_conversion(right.dtype):
elif is_ExtensionArrayDtype_and_needs_i8_conversion(left.dtype, right.dtype):
assert_extension_array_equal(
left._values,
right._values,
check_dtype=check_dtype,
index_values=np.asarray(left.index),
)
elif needs_i8_conversion(left.dtype) and needs_i8_conversion(right.dtype):
# DatetimeArray or TimedeltaArray
assert_extension_array_equal(
left._values,
Expand Down Expand Up @@ -1866,6 +1873,18 @@ def assert_copy(iter1, iter2, **eql_kwargs):
assert elem1 is not elem2, msg


def is_ExtensionArrayDtype_and_needs_i8_conversion(left_dtype, right_dtype):
Copy link
Contributor

Choose a reason for hiding this comment

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

can you rename to

is_extension_array_dtype_and_needs_i8_conversion (e.g. all lowercase)

ideally type left_dtype, right_dtype (I think we type these in other places)

Copy link
Contributor

Choose a reason for hiding this comment

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

and type the return type as bool

"""
Checks that we have the combination of an ExtensionArraydtype and
Copy link
Contributor

Choose a reason for hiding this comment

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

i would just check a single dtype and do the or condition in on L1459

a dtype that should be converted to int64

Related to issue #37609
"""
return (
is_extension_array_dtype(left_dtype) and needs_i8_conversion(right_dtype)
) or (is_extension_array_dtype(right_dtype) and needs_i8_conversion(left_dtype))


def getCols(k):
return string.ascii_uppercase[:k]

Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/util/test_assert_frame_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,20 @@ def test_assert_frame_equal_ignore_extension_dtype_mismatch(right_dtype):
tm.assert_frame_equal(left, right, check_dtype=False)


@pytest.mark.parametrize(
"dtype",
[
("timedelta64[ns]"),
("datetime64[ns, UTC]"),
("Period[D]"),
],
)
def test_assert_frame_equal_datetime_like_dtype_mismatch(dtype):
df1 = DataFrame({"a": []}, dtype=dtype)
df2 = DataFrame({"a": []})
tm.assert_frame_equal(df1, df2, check_dtype=False)


def test_allows_duplicate_labels():
left = DataFrame()
right = DataFrame().set_flags(allows_duplicate_labels=False)
Expand Down