Skip to content

Commit 0ecb8c2

Browse files
committed
Add failing test
1 parent fc9fdba commit 0ecb8c2

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

pandas/_libs/testing.pyx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ cpdef assert_almost_equal(a, b,
9999
return True
100100

101101
a_is_ndarray = is_array(a)
102+
a_has_size_and_shape = hasattr(a, "size") and hasattr(a, "shape")
102103
b_is_ndarray = is_array(b)
104+
b_has_size_and_shape = hasattr(b, "size") and hasattr(b, "shape")
103105

104106
if obj is None:
105107
if a_is_ndarray or b_is_ndarray:
@@ -119,7 +121,7 @@ cpdef assert_almost_equal(a, b,
119121
f"Can't compare objects without length, one or both is invalid: ({a}, {b})"
120122
)
121123

122-
if a_is_ndarray and b_is_ndarray:
124+
if (a_is_ndarray and b_is_ndarray) or (a_has_size_and_shape and b_has_size_and_shape):
123125
na, nb = a.size, b.size
124126
if a.shape != b.shape:
125127
from pandas._testing import raise_assert_detail

pandas/tests/dtypes/test_inference.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,50 @@ def coerce(request):
6060
return request.param
6161

6262

63+
class MockNumpyLikeArray:
64+
"""
65+
A class which is numpy-like (e.g. Pint's Quantity) but not actually numpy
66+
67+
The key is that it is not actually a numpy array so
68+
``util.is_array(mock_numpy_like_array_instance)`` returns ``False``. Other
69+
important properties are that the class defines a :meth:`__iter__` method
70+
(so that ``isinstance(abc.Iterable)`` returns ``True``) and has a
71+
:meth:`ndim` property which can be used as a check for whether it is a
72+
scalar or not.
73+
"""
74+
75+
def __init__(self, values):
76+
self._values = values
77+
78+
def __iter__(self):
79+
iter_values = iter(self._values)
80+
81+
def it_outer():
82+
for element in iter_values:
83+
yield element
84+
85+
return it_outer()
86+
87+
def __len__(self):
88+
return len(self._values)
89+
90+
@property
91+
def ndim(self):
92+
return self._values.ndim
93+
94+
@property
95+
def dtype(self):
96+
return self._values.dtype
97+
98+
@property
99+
def size(self):
100+
return self._values.size
101+
102+
@property
103+
def shape(self):
104+
return self._values.shape
105+
106+
63107
# collect all objects to be tested for list-like-ness; use tuples of objects,
64108
# whether they are list-like or not (special casing for sets), and their ID
65109
ll_params = [
@@ -166,6 +210,14 @@ class DtypeList(list):
166210
assert not inference.is_array_like(123)
167211

168212

213+
@pytest.mark.parametrize("eg", (
214+
np.array(2),
215+
MockNumpyLikeArray(np.array(2)),
216+
))
217+
def test_assert_almost_equal(eg):
218+
tm.assert_almost_equal(eg, eg)
219+
220+
169221
@pytest.mark.parametrize(
170222
"inner",
171223
[

0 commit comments

Comments
 (0)