Skip to content

fix(layer): cast polars decimal columns to float#1096

Merged
has2k1 merged 2 commits into
has2k1:mainfrom
arpitjain099:chore/polars-decimal-scales
Jul 8, 2026
Merged

fix(layer): cast polars decimal columns to float#1096
has2k1 merged 2 commits into
has2k1:mainfrom
arpitjain099:chore/polars-decimal-scales

Conversation

@arpitjain099

Copy link
Copy Markdown
Contributor

A polars Decimal column comes through to_pandas() as a pandas object column of decimal.Decimal values, so plotnine reads it as discrete and a continuous scale rejects it with "Discrete value supplied to continuous scale" (#1033). This casts those Decimal object columns to float right after the conversion, following the cheap-hack approach you mentioned in the issue, so such a column now behaves like any other numeric one instead of needing a manual cast to Float. I added a small regression test that builds a plot from a Decimal column on a continuous scale. Thanks!

polars' to_pandas() returns a Decimal column as a pandas object column
of decimal.Decimal values, which plotnine treats as discrete so a
continuous scale rejects it. Cast those object columns to float right
after the conversion so they behave like any other numeric column.

Closes has2k1#1033

Signed-off-by: Arpit Jain <arpitjain099@gmail.com>

@has2k1 has2k1 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The root cause is pandas frames with Decimal columns, so we should fix that.

Comment thread plotnine/layer.py Outdated
)


def _convert_decimal_columns(data: pd.DataFrame) -> pd.DataFrame:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to _decimal_columns_to_float and put function at the bottom of the module.

Comment thread plotnine/layer.py Outdated
Comment thread plotnine/layer.py
Comment thread tests/test_ggplot_internals.py Outdated
Move the fix to the root cause: any pandas frame with a decimal.Decimal
object column now gets that column cast to float once, right where the
layer data is finalized in _make_layer_data. This drops the per-call-site
casts on the polars to_pandas() paths and covers plain pandas frames too.

The helper _decimal_columns_to_float uses a notna() mask instead of
dropna() to peek at the first non-null value, since it runs for every
dataframe and most object columns are strings.

Closes has2k1#1033

Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
@arpitjain099

Copy link
Copy Markdown
Contributor Author

Thanks, that makes sense. I moved the fix to the root: self.data = _decimal_columns_to_float(self.data) now runs once at the end of _make_layer_data, so every dataframe is covered and the per-call-site casts on the polars paths are gone. I renamed the helper, put it at the bottom of the module, and switched to the notna() mask so we don't pay for dropna() on every object column. The test is now a plain pandas frame with a Decimal column, and the full test file passes.

@codecov

codecov Bot commented Jul 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.39%. Comparing base (bb60820) to head (9b4bd8d).
⚠️ Report is 9 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1096      +/-   ##
==========================================
- Coverage   87.46%   87.39%   -0.07%     
==========================================
  Files         208      210       +2     
  Lines       14703    14920     +217     
  Branches     1834     1889      +55     
==========================================
+ Hits        12860    13040     +180     
- Misses       1274     1302      +28     
- Partials      569      578       +9     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@has2k1 has2k1 merged commit 10aef3a into has2k1:main Jul 8, 2026
9 checks passed
@has2k1

has2k1 commented Jul 8, 2026

Copy link
Copy Markdown
Owner

@arpitjain099, thank you.

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.

2 participants