Skip to content

Add to_scipy_sparse/from_scipy_sparse xarray accessor methods - #1621

Open
aroaxinping wants to merge 3 commits into
pyjanitor-devs:devfrom
aroaxinping:feat/xarray-scipy-sparse
Open

Add to_scipy_sparse/from_scipy_sparse xarray accessor methods#1621
aroaxinping wants to merge 3 commits into
pyjanitor-devs:devfrom
aroaxinping:feat/xarray-scipy-sparse

Conversation

@aroaxinping

Copy link
Copy Markdown

Closes #636

What

Adds to_scipy_sparse and from_scipy_sparse as xarray DataArray
accessor methods, converting between a 2D DataArray and a
scipy.sparse matrix via the sparse package's COO format.

Heads up: new optional dependency

This pulls in sparse as a new optional extra (pip install pyjanitor[sparse]) — nothing changes for anyone who doesn't use it.
Followed the same try/except pattern already used for biopython/tqdm
in biology.py/chemistry.py: without sparse installed, import janitor still works fine, you just get a warning, and only calling
the new functions raises an error.

Notes

  • from_scipy_sparse wasn't in the original issue, only the to_
    direction had a sketch. I built it as a thin wrapper around the
    existing clone_using, since a scipy sparse matrix generally won't
    share da's shape — happy to change the API if you had something
    different in mind.
  • Tested with and without sparse installed either way, import janitor doesn't break.

Converts between a 2-dimensional DataArray and a scipy.sparse matrix
via the sparse package's COO format. Adds sparse as a new optional
extra, following the same try/except pattern already used for
biopython/tqdm in biology.py/chemistry.py.
CI failed because pixi.lock was out of sync with the new sparse
optional-dependency entries in pyproject.toml. Regenerated the lock
file, and added a dedicated pixi feature/environment for sparse
(mirroring biology/chemistry/engineering/spark), since the default
test environments don't install optional domain packages.

Also added the same skipif guard used in tests/biology and
tests/chemistry, so the new tests skip cleanly instead of erroring
in environments where sparse isn't installed.
Same issue as the pytest failures: the docstring examples for
to_scipy_sparse/from_scipy_sparse call sparse-backed code directly,
so they fail with NameError in environments where sparse isn't
installed (the default test environments, matching biopython/tqdm).
Marked the affected lines +SKIP, same as join_fasta's docstring in
biology.py.
@codecov

codecov Bot commented Jul 27, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 56.25000% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.82%. Comparing base (901f4b3) to head (e63807b).
⚠️ Report is 192 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1621      +/-   ##
==========================================
- Coverage   87.56%   85.82%   -1.74%     
==========================================
  Files          95      125      +30     
  Lines        6819     9928    +3109     
==========================================
+ Hits         5971     8521    +2550     
- Misses        848     1407     +559     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

[ENH] Expose sparse library functions

1 participant