Skip to content

Comments

Fix pandas Series used as numpy array index#435

Merged
vitkl merged 1 commit intomasterfrom
fix-pandas-series-nonzero
Feb 22, 2026
Merged

Fix pandas Series used as numpy array index#435
vitkl merged 1 commit intomasterfrom
fix-pandas-series-nonzero

Conversation

@vitkl
Copy link
Contributor

@vitkl vitkl commented Feb 22, 2026

Summary

Fixes #424 - AttributeError: 'Series' object has no attribute 'nonzero'

Newer pandas versions removed .nonzero() from Series objects. When a pandas Series boolean mask is used directly to index numpy/sparse arrays, it fails. The fix is to convert to numpy arrays first.

Changes across 3 files:

  • docs/notebooks/tutorial_utils.py: Add .values to slide.var["SYMBOL"] == genes[j] comparison
  • cell2location/run_colocation.py: Add .values to sp_data.obs[...] == s comparison
  • cell2location/plt/plot_expected_vs_obs.py: Wrap data.var_names == gene in np.array()

This supersedes #425 by fixing all similar occurrences in the codebase, not just the one in tutorial_utils.py.

Test plan

  • Pre-commit hooks pass (black, flake8, isort)
  • CI tests pass

🤖 Generated with Claude Code

Newer pandas versions removed .nonzero() from Series, causing
AttributeError when a Series boolean mask is used to index arrays.

- tutorial_utils.py: add .values to Series comparison before array indexing
- run_colocation.py: add .values to obs column comparison
- plot_expected_vs_obs.py: wrap var_names comparison in np.array()

Fixes #424

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@vitkl vitkl merged commit a011793 into master Feb 22, 2026
3 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.

cell2location_tutorial.ipynb cell 29 not working for 'Series' object has no attribute 'nonzero

1 participant