Skip to content

Commit d827e34

Browse files
committed
TST: GH35131 Add failing test of numpy-like array handling
1 parent 91abd0a commit d827e34

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

pandas/tests/dtypes/test_inference.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,30 @@ 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+
for element in iter(self._values):
80+
yield element
81+
82+
@property
83+
def ndim(self):
84+
return self._values.ndim
85+
86+
6387
# collect all objects to be tested for list-like-ness; use tuples of objects,
6488
# whether they are list-like or not (special casing for sets), and their ID
6589
ll_params = [
@@ -94,6 +118,15 @@ def coerce(request):
94118
(np.ndarray((2,) * 4), True, "ndarray-4d"),
95119
(np.array([[[[]]]]), True, "ndarray-4d-empty"),
96120
(np.array(2), False, "ndarray-0d"),
121+
(MockNumpyLikeArray(np.ndarray((2,) * 1)), True, "duck-ndarray-1d"),
122+
(MockNumpyLikeArray(np.array([])), True, "duck-ndarray-1d-empty"),
123+
(MockNumpyLikeArray(np.ndarray((2,) * 2)), True, "duck-ndarray-2d"),
124+
(MockNumpyLikeArray(np.array([[]])), True, "duck-ndarray-2d-empty"),
125+
(MockNumpyLikeArray(np.ndarray((2,) * 3)), True, "duck-ndarray-3d"),
126+
(MockNumpyLikeArray(np.array([[[]]])), True, "duck-ndarray-3d-empty"),
127+
(MockNumpyLikeArray(np.ndarray((2,) * 4)), True, "duck-ndarray-4d"),
128+
(MockNumpyLikeArray(np.array([[[[]]]])), True, "duck-ndarray-4d-empty"),
129+
(MockNumpyLikeArray(np.array(2)), False, "duck-ndarray-0d"),
97130
(1, False, "int"),
98131
(b"123", False, "bytes"),
99132
(b"", False, "bytes-empty"),

0 commit comments

Comments
 (0)