Skip to content

Commit da10947

Browse files
xhochyTomAugspurger
authored andcommitted
BUG: Handle nested arrays in array_equivalent_object (pandas-dev#30842)
* BUG: Handle nested arrays in array_equivalent_object
1 parent 0540671 commit da10947

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,7 @@ Other
10741074
- Bug in :class:`DataFrame` constructor when passing a 2D ``ndarray`` and an extension dtype (:issue:`12513`)
10751075
- Bug in :meth:`DataFrame.to_csv` when supplied a series with a ``dtype="string"`` and a ``na_rep``, the ``na_rep`` was being truncated to 2 characters. (:issue:`29975`)
10761076
- Bug where :meth:`DataFrame.itertuples` would incorrectly determine whether or not namedtuples could be used for dataframes of 255 columns (:issue:`28282`)
1077+
- Handle nested NumPy ``object`` arrays in :func:`testing.assert_series_equal` for ExtensionArray implementations (:issue:`30841`)
10771078

10781079
.. ---------------------------------------------------------------------------
10791080

pandas/_libs/lib.pyx

+6-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ PyDateTime_IMPORT
1919

2020
import numpy as np
2121
cimport numpy as cnp
22-
from numpy cimport (ndarray, PyArray_GETITEM,
22+
from numpy cimport (ndarray, PyArray_Check, PyArray_GETITEM,
2323
PyArray_ITER_DATA, PyArray_ITER_NEXT, PyArray_IterNew,
2424
flatiter, NPY_OBJECT,
2525
int64_t, float32_t, float64_t,
@@ -524,8 +524,11 @@ def array_equivalent_object(left: object[:], right: object[:]) -> bool:
524524
# we are either not equal or both nan
525525
# I think None == None will be true here
526526
try:
527-
if not (PyObject_RichCompareBool(x, y, Py_EQ) or
528-
(x is None or is_nan(x)) and (y is None or is_nan(y))):
527+
if PyArray_Check(x) and PyArray_Check(y):
528+
if not array_equivalent_object(x, y):
529+
return False
530+
elif not (PyObject_RichCompareBool(x, y, Py_EQ) or
531+
(x is None or is_nan(x)) and (y is None or is_nan(y))):
529532
return False
530533
except TypeError as err:
531534
# Avoid raising TypeError on tzawareness mismatch

pandas/tests/dtypes/test_missing.py

+5
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,11 @@ def test_array_equivalent():
294294
np.array([np.nan, None], dtype="object"),
295295
np.array([np.nan, None], dtype="object"),
296296
)
297+
# Check the handling of nested arrays in array_equivalent_object
298+
assert array_equivalent(
299+
np.array([np.array([np.nan, None], dtype="object"), None], dtype="object"),
300+
np.array([np.array([np.nan, None], dtype="object"), None], dtype="object"),
301+
)
297302
assert array_equivalent(
298303
np.array([np.nan, 1 + 1j], dtype="complex"),
299304
np.array([np.nan, 1 + 1j], dtype="complex"),

0 commit comments

Comments
 (0)