Skip to content

Commit bda4bc3

Browse files
authored
BUG: assert_frame_equal exception for datetime #37609 (#38157)
1 parent cb04ad7 commit bda4bc3

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

doc/source/whatsnew/v1.2.0.rst

+3
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,9 @@ Other
831831
- Bug in ``dir`` where ``dir(obj)`` wouldn't show attributes defined on the instance for pandas objects (:issue:`37173`)
832832
- Bug in :meth:`Index.drop` raising ``InvalidIndexError`` when index has duplicates (:issue:`38051`)
833833
- Bug in :meth:`RangeIndex.difference` returning :class:`Int64Index` in some cases where it should return :class:`RangeIndex` (:issue:`38028`)
834+
- Fixed bug in :func:`assert_series_equal` when comparing a datetime-like array with an equivalent non extension dtype array (:issue:`37609`)
835+
836+
834837

835838
.. ---------------------------------------------------------------------------
836839

pandas/_testing.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -1456,7 +1456,16 @@ def assert_series_equal(
14561456
check_dtype=check_dtype,
14571457
index_values=np.asarray(left.index),
14581458
)
1459-
elif needs_i8_conversion(left.dtype) or needs_i8_conversion(right.dtype):
1459+
elif is_extension_array_dtype_and_needs_i8_conversion(
1460+
left.dtype, right.dtype
1461+
) or is_extension_array_dtype_and_needs_i8_conversion(right.dtype, left.dtype):
1462+
assert_extension_array_equal(
1463+
left._values,
1464+
right._values,
1465+
check_dtype=check_dtype,
1466+
index_values=np.asarray(left.index),
1467+
)
1468+
elif needs_i8_conversion(left.dtype) and needs_i8_conversion(right.dtype):
14601469
# DatetimeArray or TimedeltaArray
14611470
assert_extension_array_equal(
14621471
left._values,
@@ -1866,6 +1875,20 @@ def assert_copy(iter1, iter2, **eql_kwargs):
18661875
assert elem1 is not elem2, msg
18671876

18681877

1878+
def is_extension_array_dtype_and_needs_i8_conversion(left_dtype, right_dtype) -> bool:
1879+
"""
1880+
Checks that we have the combination of an ExtensionArraydtype and
1881+
a dtype that should be converted to int64
1882+
1883+
Returns
1884+
-------
1885+
bool
1886+
1887+
Related to issue #37609
1888+
"""
1889+
return is_extension_array_dtype(left_dtype) and needs_i8_conversion(right_dtype)
1890+
1891+
18691892
def getCols(k):
18701893
return string.ascii_uppercase[:k]
18711894

pandas/tests/util/test_assert_frame_equal.py

+14
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,20 @@ def test_assert_frame_equal_ignore_extension_dtype_mismatch(right_dtype):
272272
tm.assert_frame_equal(left, right, check_dtype=False)
273273

274274

275+
@pytest.mark.parametrize(
276+
"dtype",
277+
[
278+
("timedelta64[ns]"),
279+
("datetime64[ns, UTC]"),
280+
("Period[D]"),
281+
],
282+
)
283+
def test_assert_frame_equal_datetime_like_dtype_mismatch(dtype):
284+
df1 = DataFrame({"a": []}, dtype=dtype)
285+
df2 = DataFrame({"a": []})
286+
tm.assert_frame_equal(df1, df2, check_dtype=False)
287+
288+
275289
def test_allows_duplicate_labels():
276290
left = DataFrame()
277291
right = DataFrame().set_flags(allows_duplicate_labels=False)

0 commit comments

Comments
 (0)