Skip to content

Conversation

@tzeitim
Copy link

@tzeitim tzeitim commented Jun 9, 2025

This PR resolves a FutureWarning related to chained assignment in pandas. The original code used:

allele_piv.loc[j[0]][j[1], cutsite] = val

This triggers a warning under newer pandas versions due to potentially unsafe chained indexing. It was replaced with the recommended form:

allele_piv.loc[j[0], (j[1], cutsite)] = val

This ensures the assignment operates on the original DataFrame, maintaining compatibility with upcoming pandas 3.0 behavior (Copy-on-Write).

No functionality is changed — this is a forward-compatibility and code quality improvement.

The above mentioned FutureWarning:


[Cassiopeia/cassiopeia/preprocess/utilities.py:525](tzeitim/Cassiopeia/cassiopeia/preprocess/utilities.py#line=524): FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0!
You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy.
A typical example is when you are setting values in a column of a DataFrame, like:

df["col"][row_indexer] = value

Use `df.loc[row_indexer, "col"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`.

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

  allele_piv.loc[j[0]][j[1], cutsite] = val

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.

1 participant