Skip to content

Fix #424, AttributeError: 'Series' object has no attribute 'nonzero'#425

Closed
huxs001 wants to merge 1 commit intoBayraktarLab:masterfrom
huxs001:master
Closed

Fix #424, AttributeError: 'Series' object has no attribute 'nonzero'#425
huxs001 wants to merge 1 commit intoBayraktarLab:masterfrom
huxs001:master

Conversation

@huxs001
Copy link
Copy Markdown

@huxs001 huxs001 commented Oct 23, 2025

Fix #424, AttributeError: 'Series' object has no attribute 'nonzero'

modified:   docs/notebooks/tutorial_utils.py

	AttributeError: 'Series' object has no attribute 'nonzero'

	modified:   docs/notebooks/tutorial_utils.py
@huxs001 huxs001 changed the title Fix #424 Fix #424, AttributeError: 'Series' object has no attribute 'nonzero' Oct 23, 2025
@vitkl
Copy link
Copy Markdown
Contributor

vitkl commented Feb 22, 2026

@claude is this the correct fix?

1 similar comment
@vitkl
Copy link
Copy Markdown
Contributor

vitkl commented Feb 22, 2026

@claude is this the correct fix?

@claude
Copy link
Copy Markdown

claude Bot commented Feb 22, 2026

Claude Code is working…

I'll analyze this and get back to you.

View job run

@vitkl
Copy link
Copy Markdown
Contributor

vitkl commented Feb 22, 2026

@claude is this the correct fix?

@claude
Copy link
Copy Markdown

claude Bot commented Feb 22, 2026

Claude finished @vitkl's task in 4m 33s —— View job


Review: Fix for AttributeError: 'Series' object has no attribute 'nonzero'

  • Gathering context
  • Analyzing the fix
  • Posting review

Analysis

The fix is incomplete / may not address the root cause.

The AttributeError: 'Series' object has no attribute 'nonzero' error is caused by this line in docs/notebooks/tutorial_utils.py:19:

np.quantile(slide.layers[n][:, slide.var["SYMBOL"] == genes[j]].toarray(), 0.992)

The expression slide.var["SYMBOL"] == genes[j] returns a pandas Series (boolean). When newer versions of scipy/numpy use this as a column selector for a sparse matrix (slide.layers[n][:, <index>]), they call .nonzero() internally — which pandas Series objects don't support (only numpy arrays do).

The correct fix is to convert the boolean Series to a numpy array:

# Add .values to convert pandas Series → numpy array
np.quantile(slide.layers[n][:, (slide.var["SYMBOL"] == genes[j]).values].toarray(), 0.992)

This pattern is already used elsewhere in the codebase (e.g., cell2location/cell_comm/around_target.py:75):

adata.var[gene_symbols].isin(genes_to_use_as_source).values

Concern about this PR: After examining the changed file's blob SHA (ec79f3ed09ecdf42578f0e271ae6eaf6244689a2), it matches the current master blob — meaning the PR's resulting file content is identical to master. The current master file at line 19 still does not have .values added. It appears this PR may not actually apply the fix.

I recommend verifying the PR diff by reviewing the actual changed line. If the .values fix is indeed present in the PR's diff (relative to its merge base), it is correct. If not, the PR needs to be updated to add .values to line 19 as shown above.

Fix this →

@vitkl
Copy link
Copy Markdown
Contributor

vitkl commented Feb 22, 2026

I used this a test for Claude - looks like it found more examples of this issue at the cost of USD 0.50 and 823,443 Sonnet 4.6 tokens - then Claude Code Opus 4.6 fixed the suggestions as part of fixed cost plan. So Claude Code is probably a better solution.

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

2 participants