BUG: Fix DataFrame.at/iat raising ValueError when storing list-like in a single cell#65311
Open
tinezivic wants to merge 2 commits intopandas-dev:mainfrom
Open
BUG: Fix DataFrame.at/iat raising ValueError when storing list-like in a single cell#65311tinezivic wants to merge 2 commits intopandas-dev:mainfrom
tinezivic wants to merge 2 commits intopandas-dev:mainfrom
Conversation
3 tasks
| not isinstance(cols_droplevel, MultiIndex) | ||
| and is_string_dtype(cols_droplevel.dtype) | ||
| and not cols_droplevel.any() | ||
| and (cols_droplevel == "").all() |
Member
There was a problem hiding this comment.
i think this is unrelated, seems to have snuck in to several of your PRs?
Contributor
Author
There was a problem hiding this comment.
Done, thanks for catching this. I’ve reverted the unrelated changes that slipped in from #62518 / #65118, including:
- the cols_droplevel check (restored to not .any())
- the allow_duplicates signature/type changes in insert()
- the allow_duplicates signature/type changes in reset_index()
- the related allow_duplicates guard logic
This PR now contains only the #61223 at/iat list-like single-cell fix. Revert commit: 36e9f68.
…n a single cell list-like values (list, tuple, ndarray, etc.) stored via .at or .iat into a column that does not yet exist or has a non-object dtype raised ValueError because the except-branch fell through to .loc/.iloc, which interprets list-like as a multi-row assignment. Fix: intercept is_list_like values before the try/except in _set_value. Ensure object dtype (cast existing column or create new one), then call _mgr.column_setitem directly. dict values are excluded (special .loc semantics). col=slice is excluded to preserve InvalidIndexError for row-level assignment (df.at[row] = list with no column specified). Closes pandas-dev#61223 STY: ruff format + fix whatsnew sort order TYP: add cast('int', icol) to column_setitem call in _set_value try block STY: wrap long line in _set_value try block (E501) STY: ruff format if condition in _set_value CLN: revert unrelated allow_duplicates and cols_droplevel changes
36e9f68 to
d418235
Compare
…ke setitem (GH#61223)
d418235 to
f519f8e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
doc/source/whatsnew/v3.1.0.rstfileAGENTS.md.Problem
DataFrame.atandDataFrame.iatraiseValueErrorwhen storing a list-likevalue (list, tuple,
np.ndarray, etc.) into a single cell of a column thateither does not yet exist or has a non-object dtype.
Root cause
_set_valueinframe.pyfalls into theexceptbranch and calls.loc[index, col] = value. pandas.locinterprets a list-like value as amulti-row assignment (set multiple rows), not a single-cell assignment, which
raises
ValueError: Must have equal len keys and value.Columns with object dtype already work because
column_setitemin thetryblock succeeds — they never reach the broken
.locfallback.Fix
Intercept list-like values (excluding
dict, which has special.locsemantics, and
slice, which is used for row-level assignment) before thetry/exceptblock. Ensure the column has object dtype first — casting if itexists with a non-object dtype, or creating a new object-dtype column if it
does not — then call
_mgr.column_setitemdirectly.AI Disclosure
I used GitHub Copilot (Claude Sonnet 4.6) to develop this pull request end-to-end:
root cause analysis, fix implementation, tests, and whatsnew entry. I prompted it
to follow
AGENTS.md. I reviewed all generated code and understand the changes made.