Fix datashader failing with single-category Categorical#539
Open
Fix datashader failing with single-category Categorical#539
Conversation
When a pd.Categorical column has only 1 category, datashader's ds.by() still produces a 3D DataArray requiring a color_key. The >1 guard was causing color_key to be None and groups to fall back to count-based aggregation, both leading to errors or incorrect output. Changes: - Change color_key guard from >1 to >0 for both shapes and points - Change groups guard from >1 to >=1 for both shapes and points - Use _hex_no_alpha in points color_key construction for consistency Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
color_vector.categories.values can contain named colors like "lightgreen", not just hex strings. Only apply _hex_no_alpha when the value starts with "#". Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
… test The test_datashader_single_group_named_color test didn't reproduce a failure on main (it only caught an intermediate regression within this PR), so remove it. Add the issue URL and detailed docstrings to the two tests that do reproduce the #483 bug on main. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The condition `groups is None or len(groups) >= 1` was always true since groups is either None or a non-empty list. Simplify to just `col_for_color is not None`, which makes the intent clearer: use categorical/reduction aggregation when there's a color column, otherwise fall back to plain count aggregation. Also note: render_labels does not use datashader, so despite the #483 issue title mentioning labels, this bug only affects shapes and points. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The color_key construction logic was duplicated between _render_shapes and _render_points. Extract into a shared helper that also caches the categories lookup and keeps the hex-vs-named-color guard in one place. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #539 +/- ##
==========================================
+ Coverage 83.83% 84.00% +0.16%
==========================================
Files 8 9 +1
Lines 2568 2600 +32
==========================================
+ Hits 2153 2184 +31
- Misses 415 416 +1
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
render_labelswith DataShader fails when coloring by a pd.Categorical with only 1 category #483: datashader raisesValueErrorwhen rendering shapes/points with apd.Categoricalcolumn that has only 1 categorycolor_keyguard from> 1to> 0so a single-category Categorical still produces a valid color key for datashadergroupsguard from> 1to>= 1so a single group uses categorical aggregation instead of falling back to count-based heatmap_hex_no_alphain pointscolor_keyconstruction for consistency with the shapes pathTest plan
test_plot_datashader_single_categoryfor shapestest_plot_datashader_single_category_pointsfor pointsmain)🤖 Generated with Claude Code