Skip to content

Commit 3d56a23

Browse files
debnathshohamCGe0516
authored andcommitted
BUG: misleading error creating df from 2d array (pandas-dev#42646)
1 parent 2b84859 commit 3d56a23

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ Groupby/resample/rolling
265265

266266
Reshaping
267267
^^^^^^^^^
268+
- Improved error message when creating a :class:`DataFrame` column from a multi-dimensional :class:`numpy.ndarray` (:issue:`42463`)
268269
- :func:`concat` creating :class:`MultiIndex` with duplicate level entries when concatenating a :class:`DataFrame` with duplicates in :class:`Index` and multiple keys (:issue:`42651`)
269270
- Bug in :meth:`pandas.cut` on :class:`Series` with duplicate indices (:issue:`42185`) and non-exact :meth:`pandas.CategoricalIndex` (:issue:`42425`)
270271
-

pandas/core/internals/construction.py

+2
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,8 @@ def _extract_index(data) -> Index:
615615
elif is_list_like(val) and getattr(val, "ndim", 1) == 1:
616616
have_raw_arrays = True
617617
raw_lengths.append(len(val))
618+
elif isinstance(val, np.ndarray) and val.ndim > 1:
619+
raise ValueError("Per-column arrays must each be 1-dimensional")
618620

619621
if not indexes and not raw_lengths:
620622
raise ValueError("If using all scalar values, you must pass an index")

pandas/tests/frame/test_constructors.py

+13
Original file line numberDiff line numberDiff line change
@@ -2530,6 +2530,19 @@ def test_from_2d_object_array_of_periods_or_intervals(self):
25302530
expected = DataFrame({0: pi, 1: ii, 2: pi, 3: ii})
25312531
tm.assert_frame_equal(df3, expected)
25322532

2533+
@pytest.mark.parametrize(
2534+
"col_a, col_b",
2535+
[
2536+
([[1], [2]], np.array([[1], [2]])),
2537+
(np.array([[1], [2]]), [[1], [2]]),
2538+
(np.array([[1], [2]]), np.array([[1], [2]])),
2539+
],
2540+
)
2541+
def test_error_from_2darray(self, col_a, col_b):
2542+
msg = "Per-column arrays must each be 1-dimensional"
2543+
with pytest.raises(ValueError, match=msg):
2544+
DataFrame({"a": col_a, "b": col_b})
2545+
25332546

25342547
class TestDataFrameConstructorWithDtypeCoercion:
25352548
def test_floating_values_integer_dtype(self):

0 commit comments

Comments
 (0)