Repository navigation
fix: multi-column groupby crashing on non-string observations - #4385
Conversation
…#4384) _prepare_dataframe joined multi-column groupby values with "_".join directly and took ordering levels from .cat.categories, so integer categoricals and plain numeric columns raised TypeError. Convert values with astype(str), fall back to np.unique for non-categorical columns, and order unmatched joined labels (NaN combinations) last.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4385 +/- ##
=======================================
Coverage 85.11% 85.11%
=======================================
Files 139 139
Lines 12318 12318
=======================================
Hits 10484 10484
Misses 1834 1834
Flags with carried forward coverage won't be shown. Click here to find out more.
|
…e#4384) _prepare_dataframe joined multi-column groupby values with "_".join directly and took ordering levels from .cat.categories, so integer categoricals and plain numeric columns raised TypeError. Join values through per-element str(), take levels from np.unique for non-categorical columns, and give rows missing a groupby value no joined category, matching the single-column path.
ebffb76 to
682a0c3
Compare
|
The only failing check is the title/milestone/labels metadata check, which needs maintainer-side labels. |
|
You're committing hundreds of times in weeks. Have you human reviewed your own PR? Otherwise, I'll close this as maintainers can guide an agent to exactly the needed solution much better. |
|
Yes, this diff was human-reviewed before posting. Multi-column groupby joined obs values with "_".join, which raised TypeError on integer or float columns. The fix maps values through str, keeps the single-column NaN rule (a row missing any key gets no category), and falls back to np.unique for ordering when a column is not categorical. The added tests run under the hatch-test CI matrices (3.12 low-vers, 3.14 stable/pre/few-extras), which pass. On commit volume, I can squash the branch if that is easier to review. |
| *( | ||
| obs_tidy[g].cat.categories | ||
| if isinstance(obs_tidy[g].dtype, CategoricalDtype) | ||
| else np.unique(obs_tidy[g]) |
There was a problem hiding this comment.
np.unique sorts object values, so this still raises for an ordinary non-categorical grouping column containing mixed strings and integers, or strings and missing values. For example, np.unique(pd.Series([1, 'control'], dtype=object)) raises TypeError: '<' not supported between instances of 'str' and 'int'; ['control', None] fails similarly. This PR is intended to support non-string observations. Could we use pd.unique/a categorical-safe unique operation (excluding missing values) and add a mixed-type and missing-value regression for multi-column grouping?
There was a problem hiding this comment.
Thanks for the correction, @barlowa124. I checked head b927638: the mixed int/string and string/missing cases now fall back to pd.unique with missing values dropped, while ordinary numeric values retain the sorted path. I independently exercised those three cases, and the new hosted test matrices pass. That resolves the correctness issue I raised. One small CI fix remains: the ruff check job reports D205 at tests/plotting/legacy/test_plotting.py:444 because the new multi-line test docstring needs a blank line between its summary and description. Appreciate the follow-through.
np.unique raises TypeError on object columns mixing ints and strings or containing missing values. Fall back to pd.unique in appearance order with missing values dropped, and cover the case with a mixed-type/missing-value regression test. Addresses review on scverse#4385.
|
Fixed in d31f6df. |
Documentation build overview
318 files changed ·
|
…g on non-string observations) (#4517) Co-authored-by: Angus Barlow <[email protected]>
|
Thanks for following up on the mixed-type case, @barlowa124, and for taking it through to merge. Glad the fallback now covers mixed string/integer and missing values, with the grouping regressions in place. Appreciate the careful correction. |
Fixes #4384
For multi-column
groupby,_prepare_dataframejoins the key valueswith
"_".joinover raw values and takes ordering levels from.cat.categories. A non-string key column (integer categorical, plainnumeric) or a NaN row then fails with
TypeError: sequence item 1: expected str instance. Single-keygroupbyhandles the same columnscorrectly. The breakage is specific to the multi-key join. The shared
helper is used by
dotplot,matrixplot,stacked_violin,tracksplot,heatmapand the dendrogram path, so they all gain thefix.
str()individually..cat.categorieson categorical columnsand
np.uniqueotherwise.single-column path where
NaNnever forms one.Tests
test_plotting.pygains parametrizedtest_multi_groupby_nonstringand
test_multi_groupby_partial_nan. Integer-categorical and numericsecond keys plus partially-NaN keys are pushed through
sc.pl.dotplotand
sc.pl.matrixplot, with joined labels asserted on the returnedplot object. The pre-existing
test_violinimage-compare failures inthis file are seaborn drift, identical on
main.