Skip to content

BUG: DataFrame.__setitem__ silently dropping columns of a 2D value (GH#46544)#66001

Draft
jbrockmendel wants to merge 1 commit into
pandas-dev:mainfrom
jbrockmendel:bug-46544
Draft

BUG: DataFrame.__setitem__ silently dropping columns of a 2D value (GH#46544)#66001
jbrockmendel wants to merge 1 commit into
pandas-dev:mainfrom
jbrockmendel:bug-46544

Conversation

@jbrockmendel

Copy link
Copy Markdown
Member

closes #46544

Setting an existing column label with a multi-column 2D array silently kept only the first column and discarded the rest:

df = pd.DataFrame(np.zeros((256, 10)))
df[0] = np.zeros((256, 2))  # silently kept column 0 of the value, dropped the rest

Every sibling path already rejected this — the new-column case (GH-46545), the list-key case, and DataFrame.loc/DataFrame.iloc all raise. The existing-column __setitem__ path was the lone outlier because BlockManager.iset validated only the row count of the (transposed) value, never its column count. iset now raises the same Expected a 1D array ValueError that insert already does.

Single-column 2D values (shape (N, 1), e.g. GH-42376) and keys that map to multiple columns (duplicate labels / partial MultiIndex keys, where an (N, k) value fills the k matched columns) are unaffected and still work.

…H#46544)

Setting an existing column label with a multi-column 2D array silently kept
only the first column. iset now raises, matching the new-column and
DataFrame.loc paths.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@jbrockmendel jbrockmendel added Bug Indexing Related to indexing on series/frames, not to indexes themselves labels Jun 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Indexing Related to indexing on series/frames, not to indexes themselves

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: DataFrame.loc is not consistent with DataFrame.__setitem__ when used with 2D numpy array

1 participant