Skip to content

Commit 78f8ce7

Browse files
committed
new fix
1 parent c5c8953 commit 78f8ce7

File tree

2 files changed

+21
-26
lines changed

2 files changed

+21
-26
lines changed

pandas/core/construction.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -566,14 +566,6 @@ def sanitize_array(
566566
# extract ndarray or ExtensionArray, ensure we have no NumpyExtensionArray
567567
data = extract_array(data, extract_numpy=True, extract_range=True)
568568

569-
# GH#61026: when 2D input is allowed (e.g. DataFrame column assignment),
570-
# treat a (n, 1) numpy array as a 1D array of length n so downstream code
571-
# (including pyarrow-backed StringArray) always sees 1D.
572-
if allow_2d and isinstance(data, np.ndarray) and data.ndim == 2:
573-
rows, cols = data.shape
574-
if cols == 1:
575-
data = data[:, 0]
576-
577569
if isinstance(data, np.ndarray) and data.ndim == 0:
578570
if dtype is None:
579571
dtype = data.dtype
@@ -619,24 +611,6 @@ def sanitize_array(
619611
data = data.A
620612

621613
if dtype is None:
622-
# GH#61026: special-case 2D+ object ndarrays when dtype is None.
623-
if allow_2d and data.dtype == object and data.ndim > 1:
624-
if data.ndim == 2 and data.shape[1] == 1:
625-
# allow assigning a (n, 1) object array to a single column.
626-
data = data[:, 0]
627-
elif data.ndim == 2:
628-
# more than 1 column, not allowed.
629-
raise ValueError(
630-
"Setting a DataFrame column with a 2D object array "
631-
f"requires shape (n, 1); got shape {data.shape}."
632-
)
633-
else:
634-
# ndim >= 3
635-
raise ValueError(
636-
f"Setting a DataFrame column with ndim {data.ndim} "
637-
"object array is not supported."
638-
)
639-
640614
subarr = data
641615
if data.dtype == object and infer_object:
642616
subarr = lib.maybe_convert_objects(

pandas/core/frame.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5501,7 +5501,28 @@ 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+
55045524
com.require_length_match(value, self.index)
5525+
55055526
return sanitize_array(value, self.index, copy=True, allow_2d=True), None
55065527

55075528
@property

0 commit comments

Comments
 (0)