Skip to content

Commit 407e67c

Browse files
authored
BUG: PandasArray._from_sequence with list-of-tuples (#39828)
1 parent 549ccb9 commit 407e67c

File tree

3 files changed

+23
-78
lines changed

3 files changed

+23
-78
lines changed

pandas/core/arrays/numpy_.py

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
)
1919
from pandas.compat.numpy import function as nv
2020

21+
from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
2122
from pandas.core.dtypes.dtypes import PandasDtype
2223
from pandas.core.dtypes.missing import isna
2324

@@ -97,6 +98,14 @@ def _from_sequence(
9798
dtype = dtype._dtype
9899

99100
result = np.asarray(scalars, dtype=dtype)
101+
if (
102+
result.ndim > 1
103+
and not hasattr(scalars, "dtype")
104+
and (dtype is None or dtype == object)
105+
):
106+
# e.g. list-of-tuples
107+
result = construct_1d_object_array_from_listlike(scalars)
108+
100109
if copy and result is scalars:
101110
result = result.copy()
102111
return cls(result)

pandas/core/internals/blocks.py

+9-24
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,17 @@ def _check_ndim(self, values, ndim):
207207
if ndim is None:
208208
ndim = values.ndim
209209

210-
if self._validate_ndim and values.ndim != ndim:
210+
if self._validate_ndim:
211+
if values.ndim != ndim:
212+
raise ValueError(
213+
"Wrong number of dimensions. "
214+
f"values.ndim != ndim [{values.ndim} != {ndim}]"
215+
)
216+
elif values.ndim > ndim:
217+
# ExtensionBlock
211218
raise ValueError(
212219
"Wrong number of dimensions. "
213-
f"values.ndim != ndim [{values.ndim} != {ndim}]"
220+
f"values.ndim > ndim [{values.ndim} > {ndim}]"
214221
)
215222
return ndim
216223

@@ -2178,28 +2185,6 @@ def fillna(
21782185
value, limit=limit, inplace=inplace, downcast=downcast
21792186
)
21802187

2181-
def _check_ndim(self, values, ndim):
2182-
"""
2183-
ndim inference and validation.
2184-
2185-
This is overridden by the DatetimeTZBlock to check the case of 2D
2186-
data (values.ndim == 2), which should only be allowed if ndim is
2187-
also 2.
2188-
The case of 1D array is still allowed with both ndim of 1 or 2, as
2189-
if the case for other EAs. Therefore, we are only checking
2190-
`values.ndim > ndim` instead of `values.ndim != ndim` as for
2191-
consolidated blocks.
2192-
"""
2193-
if ndim is None:
2194-
ndim = values.ndim
2195-
2196-
if values.ndim > ndim:
2197-
raise ValueError(
2198-
"Wrong number of dimensions. "
2199-
f"values.ndim != ndim [{values.ndim} != {ndim}]"
2200-
)
2201-
return ndim
2202-
22032188

22042189
class TimeDeltaBlock(DatetimeLikeBlockMixin):
22052190
__slots__ = ()

pandas/tests/extension/test_numpy.py

+5-54
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,6 @@ def test_getitem_scalar(self, data):
189189
# AssertionError
190190
super().test_getitem_scalar(data)
191191

192-
@skip_nested
193-
def test_take_series(self, data):
194-
# ValueError: PandasArray must be 1-dimensional.
195-
super().test_take_series(data)
196-
197192

198193
class TestGroupby(BaseNumPyTests, base.BaseGroupbyTests):
199194
def test_groupby_extension_apply(
@@ -222,13 +217,6 @@ def test_shift_fill_value(self, data):
222217
# np.array shape inference. Shift implementation fails.
223218
super().test_shift_fill_value(data)
224219

225-
@skip_nested
226-
@pytest.mark.parametrize("box", [pd.Series, lambda x: x])
227-
@pytest.mark.parametrize("method", [lambda x: x.unique(), pd.unique])
228-
def test_unique(self, data, box, method):
229-
# Fails creating expected
230-
super().test_unique(data, box, method)
231-
232220
@skip_nested
233221
def test_fillna_copy_frame(self, data_missing):
234222
# The "scalar" for this array isn't a scalar.
@@ -244,31 +232,10 @@ def test_searchsorted(self, data_for_sorting, as_series):
244232
# Test setup fails.
245233
super().test_searchsorted(data_for_sorting, as_series)
246234

247-
@skip_nested
248-
def test_where_series(self, data, na_value, as_frame):
249-
# Test setup fails.
250-
super().test_where_series(data, na_value, as_frame)
251-
252-
@pytest.mark.parametrize("repeats", [0, 1, 2, [1, 2, 3]])
253-
def test_repeat(self, data, repeats, as_series, use_numpy, request):
254-
if data.dtype.numpy_dtype == object and repeats != 0:
255-
mark = pytest.mark.xfail(reason="mask shapes mismatch")
256-
request.node.add_marker(mark)
257-
super().test_repeat(data, repeats, as_series, use_numpy)
258-
259235
@pytest.mark.xfail(reason="PandasArray.diff may fail on dtype")
260236
def test_diff(self, data, periods):
261237
return super().test_diff(data, periods)
262238

263-
@pytest.mark.parametrize("box", [pd.array, pd.Series, pd.DataFrame])
264-
def test_equals(self, data, na_value, as_series, box, request):
265-
# Fails creating with _from_sequence
266-
if box is pd.DataFrame and data.dtype.numpy_dtype == object:
267-
mark = pytest.mark.xfail(reason="AssertionError in _get_same_shape_values")
268-
request.node.add_marker(mark)
269-
270-
super().test_equals(data, na_value, as_series, box)
271-
272239

273240
class TestArithmetics(BaseNumPyTests, base.BaseArithmeticOpsTests):
274241
divmod_exc = None
@@ -289,8 +256,11 @@ def test_divmod_series_array(self, data):
289256
def test_arith_series_with_scalar(self, data, all_arithmetic_operators):
290257
super().test_arith_series_with_scalar(data, all_arithmetic_operators)
291258

292-
@skip_nested
293-
def test_arith_series_with_array(self, data, all_arithmetic_operators):
259+
def test_arith_series_with_array(self, data, all_arithmetic_operators, request):
260+
opname = all_arithmetic_operators
261+
if data.dtype.numpy_dtype == object and opname not in ["__add__", "__radd__"]:
262+
mark = pytest.mark.xfail(reason="Fails for object dtype")
263+
request.node.add_marker(mark)
294264
super().test_arith_series_with_array(data, all_arithmetic_operators)
295265

296266
@skip_nested
@@ -325,11 +295,6 @@ def test_fillna_scalar(self, data_missing):
325295
# Non-scalar "scalar" values.
326296
super().test_fillna_scalar(data_missing)
327297

328-
@skip_nested
329-
def test_fillna_series_method(self, data_missing, fillna_method):
330-
# Non-scalar "scalar" values.
331-
super().test_fillna_series_method(data_missing, fillna_method)
332-
333298
@skip_nested
334299
def test_fillna_series(self, data_missing):
335300
# Non-scalar "scalar" values.
@@ -358,20 +323,6 @@ def test_merge(self, data, na_value):
358323
# Fails creating expected (key column becomes a PandasDtype because)
359324
super().test_merge(data, na_value)
360325

361-
@skip_nested
362-
def test_merge_on_extension_array(self, data):
363-
# Fails creating expected
364-
super().test_merge_on_extension_array(data)
365-
366-
@skip_nested
367-
def test_merge_on_extension_array_duplicates(self, data):
368-
# Fails creating expected
369-
super().test_merge_on_extension_array_duplicates(data)
370-
371-
@skip_nested
372-
def test_transpose_frame(self, data):
373-
super().test_transpose_frame(data)
374-
375326

376327
class TestSetitem(BaseNumPyTests, base.BaseSetitemTests):
377328
@skip_nested

0 commit comments

Comments
 (0)