Skip to content

Dominik/pandas3 cow fix issue180#181

Merged
katosh merged 3 commits into
masterfrom
dominik/pandas3-cow-fix-issue180
Jul 15, 2026
Merged

Dominik/pandas3 cow fix issue180#181
katosh merged 3 commits into
masterfrom
dominik/pandas3-cow-fix-issue180

Conversation

@katosh

@katosh katosh commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

Fix pandas 3 Copy-on-Write compatibility (#180)

Problem

Under pandas 3, Copy-on-Write is mandatory, so DataFrame.values returns a read-only view and
in-place mutation of it raises ValueError: assignment destination is read-only. On pandas 2 (CoW off by
default) the same code worked, so the breakage is pandas-3-specific. Two functions are affected:

  • run_palantir_differentiation_entropy: builds a terminal-state identity matrix via
    bp.values[range(n), range(n)] = 1 (the site reported in Pandas 3 compatibility: ValueError: assignment destination is read-only in _differentiation_entropy #180, plus an identical occurrence in the
    len(trans_states) == 0 branch).
  • select_branch_cells: NaN-fills the fate-probability array in place. It obtains that array from
    _validate_obsm_key(..., as_df=False), which returns DataFrame.values when probabilities are stored as
    a DataFrame — the default run_palantir output — so this breaks the standard workflow.

Fix

  • Build the identity block with np.eye(...) instead of mutating a .values view (both occurrences).
  • Take an owned .copy() of the fate-probability array before the in-place NaN fill in select_branch_cells.

Both are Copy-on-Write-safe and behave identically on pandas 2 and 3. Results are unchanged; the identity
block's dtype becomes float64 (it was concatenated with float probabilities anyway).

Compatibility

No version bump; pandas 2 support retained. Verified in clean venvs (Python 3.13):

  • pandas 3.0.3 / numpy 2.4.6: tests/test_core_run_palantir.py 5/5, tests/test_presults_select_branch_cells.py 5/5.
  • pandas 2.3.3 / numpy 2.4.6: both suites 5/5 (no regression).

Changelog

Added an entry under Version 1.4.5 (future) in README.md.

Closes #180

katosh added 2 commits July 15, 2026 11:19
Build the terminal-state identity block with np.eye instead of
mutating the read-only DataFrame.values view in place. Under pandas
3 (mandatory Copy-on-Write) the old in-place assignment raised
ValueError: assignment destination is read-only. Fixes both the
main path and the trans_states==0 edge branch. Compatible with
pandas 2 and 3.
_validate_obsm_key(as_df=False) returns a DataFrame.values view that
is read-only under pandas 3 (mandatory Copy-on-Write). select_branch_cells
mutated it in place (NaN fill), raising ValueError: assignment destination
is read-only on the default SAVE_AS_DF=True workflow. Copy the array
before the in-place fill. Sole mutating as_df=False caller; the other two
(compute_gene_trends, run_low_density_variability) only read.

Amends the CoW fix to cover this second site found in the sibling sweep.
anndata >=0.11 removed the AnnData.__init__ 'dtype' argument, breaking
cluster_gene_trends on Python 3.12/3.13 (where the newer anndata
resolves) with 'TypeError: got an unexpected keyword argument dtype'.
Cast the array to float32 before construction instead — behaviour is
identical and compatible with all anndata versions and pandas 2 & 3.
@katosh
katosh merged commit 9bdd700 into master Jul 15, 2026
5 checks passed
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.

Pandas 3 compatibility: ValueError: assignment destination is read-only in _differentiation_entropy

1 participant