Skip to content

Commit e2ad3fb

Browse files
committed
trying new patch location and logic & revamped test infra
1 parent 78f8ce7 commit e2ad3fb

File tree

3 files changed

+41
-74
lines changed

3 files changed

+41
-74
lines changed

pandas/core/frame.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5501,29 +5501,31 @@ def _sanitize_column(self, value) -> tuple[ArrayLike, BlockValuesRefs | None]:
55015501
return _reindex_for_setitem(value, self.index)
55025502

55035503
if is_list_like(value):
5504-
# GH#61026: this method is only used for *single-column* assignment.
5505-
# Reject 2D/3D arrays here, except the (n, 1) case which we treat as 1D.
5506-
if isinstance(value, np.ndarray) and value.ndim > 1:
5507-
if value.ndim == 2:
5508-
if value.shape[1] == 1:
5509-
# (n, 1) → length-n 1D array
5510-
value = value[:, 0]
5511-
else:
5512-
# More than one column: users should use df[[...]] = value
5513-
raise ValueError(
5514-
"Setting a DataFrame column with a 2D array requires "
5515-
f"shape (n, 1); got shape {value.shape}."
5516-
)
5517-
else:
5518-
# ndim >= 3
5519-
raise ValueError(
5520-
f"Setting a DataFrame column with ndim {value.ndim} "
5521-
"array is not supported."
5522-
)
5523-
55245504
com.require_length_match(value, self.index)
55255505

5526-
return sanitize_array(value, self.index, copy=True, allow_2d=True), None
5506+
# GH#61026: special-case 2D inputs for single-column assignment.
5507+
# - accept shape (n, 1) by flattening to 1D
5508+
# - disallow 2D *object* arrays with more than one column, since those
5509+
# correspond to a single column key and should be rejected
5510+
arr = value
5511+
5512+
# np.matrix is always 2D; gonna convert to regular ndarray
5513+
if isinstance(arr, np.matrix):
5514+
arr = np.asarray(arr)
5515+
5516+
if isinstance(arr, np.ndarray) and arr.ndim == 2:
5517+
if arr.shape[1] == 1:
5518+
# treating (n, 1) as a length-n 1D array
5519+
arr = arr[:, 0]
5520+
elif arr.dtype == object:
5521+
# single-column setitem with a 2D object array is not allowed.
5522+
msg = (
5523+
"Setting a DataFrame column with a 2D array requires "
5524+
f"shape (n, 1); got shape {arr.shape}."
5525+
)
5526+
raise ValueError(msg)
5527+
subarr = sanitize_array(arr, self.index, copy=True, allow_2d=True)
5528+
return subarr, None
55275529

55285530
@property
55295531
def _series(self):

pandas/tests/frame/indexing/test_setitem.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,24 @@ def test_setitem_index_object_dtype_not_inferring(self):
816816
)
817817
tm.assert_frame_equal(df, expected)
818818

819+
def test_setitem_2d_object_array(self):
820+
# GH#61026
821+
df = DataFrame(
822+
{
823+
"c1": [1, 2, 3, 4, 5],
824+
}
825+
)
826+
827+
arr = np.array([["A"], ["B"], ["C"], ["D"], ["E"]], dtype=object)
828+
df["c1"] = arr
829+
830+
expected = DataFrame(
831+
{
832+
"c1": ["A", "B", "C", "D", "E"],
833+
}
834+
)
835+
tm.assert_frame_equal(df, expected)
836+
819837

820838
class TestSetitemTZAwareValues:
821839
@pytest.fixture

pandas/tests/frame/indexing/test_setitem_2d_object.py

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)