Skip to content

Fix add_peak_annotation crash on empty distance values#182

Open
LiudengZhang wants to merge 2 commits intoscverse:mainfrom
LiudengZhang:fix/empty-distance-peak-annotation
Open

Fix add_peak_annotation crash on empty distance values#182
LiudengZhang wants to merge 2 commits intoscverse:mainfrom
LiudengZhang:fix/empty-distance-peak-annotation

Conversation

@LiudengZhang
Copy link

Description

Fix add_peak_annotation crashing when the peak annotation TSV contains empty distance values (intergenic peaks).

Root cause: When the distance column contains a mix of numeric values and empty strings, pd.read_csv infers it as float64. The existing code then tries to assign "" into a numeric column, triggering LossySetitemError. On newer pandas with StringDtype, a similar issue occurs with pd.NA values.

Fix:

  • Convert distance column to string via astype(object).fillna("").astype(str) to safely handle both numeric and nullable string dtypes
  • Replace the multi-step ""0floatint conversion with pd.to_numeric(errors="coerce").fillna(0).astype(int), which handles all edge cases in one expression

How has this been tested?

  • Added regression test with exact data from the bug report (intergenic peaks with empty distance)
  • Added test for semicolon-separated multi-gene annotations
  • Existing tests pass (pytest tests/ — 13 passed)

Closes

Closes #181

When the distance column in peak annotation TSV contains empty values
(intergenic peaks), pandas may infer a numeric dtype. This caused
LossySetitemError when trying to assign "" into the numeric column.

Fix by converting distance to string via object dtype early, and using
pd.to_numeric with errors="coerce" for the final int conversion,
which gracefully handles empty/NaN values as 0.

Add regression tests for empty distance values and semicolon-separated
multi-gene annotations.

Closes scverse#181
@ilia-kats
Copy link
Collaborator

Thank you for the fix! I've pushed a slight update to your branch: Given how long ago Pandas 1.0 was released, we can assume that people using a somewhat up to date muon have it, and get rid of all this string casting nonsense and just use pd.NA throughout. It passes the tests, but if you have some real data lying around, please also test it with that. Otherwise I'll just merge this.

@LiudengZhang
Copy link
Author

Thanks for the cleanup — the pd.NA approach is much cleaner. Pulled and verified tests pass locally. Looks good to me!

@LiudengZhang
Copy link
Author

Thanks for the cleanup! Feel free to merge.

Pandas 1.0 has been released sufficiently long ago that we can assume
muon users have it.
@ilia-kats ilia-kats force-pushed the fix/empty-distance-peak-annotation branch from 8d28154 to eca6756 Compare March 12, 2026 14:33
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.

add_peak_annotation can't handle empty values in distance column, enforces numeric typing

2 participants