Skip to content

[SPARK-58013][PS] Fix loc setitem dropping writes for reordered columns on pandas 3#57504

Open
Vivek1106-04 wants to merge 2 commits into
apache:masterfrom
Vivek1106-04:SPARK-58013-pandas-tests
Open

[SPARK-58013][PS] Fix loc setitem dropping writes for reordered columns on pandas 3#57504
Vivek1106-04 wants to merge 2 commits into
apache:masterfrom
Vivek1106-04:SPARK-58013-pandas-tests

Conversation

@Vivek1106-04

Copy link
Copy Markdown

What changes were proposed in this pull request?

Removes an early-return in LocIndexerLike.__setitem__ (python/pyspark/pandas/indexing.py) that, under pandas 3, turned a scalar .loc assignment into a no-op when the target columns were passed in a different order than the frame's internal column order.

Why are the changes needed?

The branch (added in SPARK-55296) assumed pandas 3 does not apply such writes. It does, e.g. df.loc[["viper", "sidewinder"], ["shield", "max_speed"]] = 10 sets both columns. The guard silently dropped valid writes, failing test_frame_loc_setitem (op1) on pandas 3. For a scalar assignment the column order is irrelevant, and the CoW view-decoupling the branch was meant to protect is already handled by the normal assignment path.

Does this PR introduce any user-facing change?

Yes. With pandas 3, DataFrame.loc[rows, cols] = scalar now correctly applies when cols is a list in non-internal order; previously it was a silent no-op. No change on pandas 2 (the removed code was behind a pandas >= 3.0.0 check).

How was this patch tested?

Ran pyspark.pandas.tests.indexes.test_indexing_loc (classic and Connect parity) on pandas 3.0.5. The previously failing test_frame_loc_setitem passes, and the full indexes suite is green with no regressions.

Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Code

…ns on pandas 3

The pandas 3 CoW branch in `LocIndexerLike.__setitem__` returned early
(no-op) when a scalar was assigned to a list of columns not in the
frame's internal order. pandas 3 actually applies such writes, so the
guard silently dropped valid assignments. Remove it; both column orders
now use the normal path. The change is behind `pandas >= 3.0.0`, so
pandas 2 is unaffected.
@Vivek1106-04
Vivek1106-04 force-pushed the SPARK-58013-pandas-tests branch from f46e9a2 to 0753c3a Compare July 24, 2026 16:25
Comment thread python/pyspark/pandas/indexing.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the deletion, selected_column_labels is unpacked but never used anywhere else in the method (its only other references were inside the removed block; missing_keys handling uses missing_keys/data_spark_columns, not this name). Please revert the unpack to _, data_spark_columns, _, _, _ so the now-misleading name does not imply it is live.

@uros-b uros-b left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @Vivek1106-04 for the cleanup, I left one suggestion, otherwise looks good

@Vivek1106-04
Vivek1106-04 requested a review from uros-b July 25, 2026 07:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants