Skip to content

Commit dcf6b66

Browse files
committed
TST: GH35131 Add failing test of numpy-like array handling
1 parent 19e8fcc commit dcf6b66

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
@@ -59,6 +59,30 @@ def coerce(request):
5959
return request.param
6060

6161

62+
class MockNumpyLikeArray:
63+
"""
64+
A class which is numpy-like (e.g. Pint's Quantity) but not actually numpy
65+
66+
The key is that it is not actually a numpy array so
67+
``util.is_array(mock_numpy_like_array_instance)`` returns ``False``. Other
68+
important properties are that the class defines a :meth:`__iter__` method
69+
(so that ``isinstance(abc.Iterable)`` returns ``True``) and has a
70+
:meth:`ndim` property which can be used as a check for whether it is a
71+
scalar or not.
72+
"""
73+
74+
def __init__(self, values):
75+
self._values = values
76+
77+
def __iter__(self):
78+
for element in iter(self._values):
79+
yield element
80+
81+
@property
82+
def ndim(self):
83+
return self._values.ndim
84+
85+
6286
# collect all objects to be tested for list-like-ness; use tuples of objects,
6387
# whether they are list-like or not (special casing for sets), and their ID
6488
ll_params = [
@@ -93,6 +117,15 @@ def coerce(request):
93117
(np.ndarray((2,) * 4), True, "ndarray-4d"),
94118
(np.array([[[[]]]]), True, "ndarray-4d-empty"),
95119
(np.array(2), False, "ndarray-0d"),
120+
(MockNumpyLikeArray(np.ndarray((2,) * 1)), True, "duck-ndarray-1d"),
121+
(MockNumpyLikeArray(np.array([])), True, "duck-ndarray-1d-empty"),
122+
(MockNumpyLikeArray(np.ndarray((2,) * 2)), True, "duck-ndarray-2d"),
123+
(MockNumpyLikeArray(np.array([[]])), True, "duck-ndarray-2d-empty"),
124+
(MockNumpyLikeArray(np.ndarray((2,) * 3)), True, "duck-ndarray-3d"),
125+
(MockNumpyLikeArray(np.array([[[]]])), True, "duck-ndarray-3d-empty"),
126+
(MockNumpyLikeArray(np.ndarray((2,) * 4)), True, "duck-ndarray-4d"),
127+
(MockNumpyLikeArray(np.array([[[[]]]])), True, "duck-ndarray-4d-empty"),
128+
(MockNumpyLikeArray(np.array(2)), False, "duck-ndarray-0d"),
96129
(1, False, "int"),
97130
(b"123", False, "bytes"),
98131
(b"", False, "bytes-empty"),

0 commit comments

Comments
 (0)