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 3 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
14 changes: 13 additions & 1 deletion pandas/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1456,7 +1456,19 @@ 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_extension_array_dtype(left.dtype) and needs_i8_conversion(right.dtype)
) or (is_extension_array_dtype(right.dtype) and needs_i8_conversion(left.dtype)):
# If we have an extension array and Datetimearray / TimedeltaArray, we still
# want to assert_extension_array_equal even though Datetimearray and
Copy link
Contributor

Choose a reason for hiding this comment

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

this is not correct, the dtypes of a DTA or TDA are not EA dtypes (well except a tz-aware DTA).

    >>> is_extension_type(pd.DatetimeIndex([1, 2, 3]))
    False
    >>> is_extension_type(pd.DatetimeIndex([1, 2, 3], tz="US/Eastern"))
    True

the check is ok, its the text that needs updating.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @jreback !
Thanks for the quick response. Are you saying that just the comment needs to be changed?
Maybe to:

If we have an extension array dtype and Datetime dtype / Timedelta dtype..

I'm a bit confused otherwise because I say later in the comment that

Datetimearray and TimedeltaArray dtypes are not an instance of ExtensionArraydtype subclass

Copy link
Contributor

Choose a reason for hiding this comment

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

see my comment again. its not about DTA or TDA themselve, rather their dtypes

Copy link

@RichardWong8 RichardWong8 Nov 29, 2020

Choose a reason for hiding this comment

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

@jreback
Would this suffice?

If we have an array of dtype EA and a DTA/TDA (whose dtype is not of EA), we still perform assert_extension_array_equal

Copy link
Member

Choose a reason for hiding this comment

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

There is a lot going on here.
I suggest extracting a function checking for this condition, with a proper docstring.

Copy link
Member

Choose a reason for hiding this comment

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

You may also want to reference original issue in the comment or docstring (#37609).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay! Will do.

# TimedeltaArray dtypes are not an instance of ExtensionArraydtype subclass
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
6 changes: 6 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,12 @@ def test_assert_frame_equal_ignore_extension_dtype_mismatch(right_dtype):
tm.assert_frame_equal(left, right, check_dtype=False)


def test_assert_frame_equal_datetime_dtype_mismatch():
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 parameterize over timedelta, datetime64[ns, UTC], Period[D]

df1 = DataFrame({"a": []}, dtype="datetime64[ns]")
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