Skip to content

feat: Added drop_nulls option to to_dummies #23215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

florian-klein
Copy link
Contributor

@florian-klein florian-klein commented Jun 17, 2025

Motivation

I felt like it would be very useful to have this option, instead of manually dropping resulting null columns after calling to_dummies. One advantage also is that the null column is not materialized at all.

Edit: This also fixes #19095. After reading through #19325, I'm not sure if this PR is actually worthwhile moving forward. Imho I think adding this optional parameter makes a lot of sense, but let me know if you feel differently

Example:

import polars as pl

s = pl.Series("a", [1, 2, None])

print("Keep Null Column", s.to_dummies()) # current and default behavior
print("Drop Null Column", s.to_dummies(drop_nulls=True))
Keep Null Column shape: (3, 3)
┌─────┬─────┬────────┐
│ a_1 ┆ a_2 ┆ a_null │
│ --- ┆ --- ┆ ---    │
│ u8  ┆ u8  ┆ u8     │
╞═════╪═════╪════════╡
│ 1   ┆ 0   ┆ 0      │
│ 0   ┆ 1   ┆ 0      │
│ 0   ┆ 0   ┆ 1      │
└─────┴─────┴────────┘

Drop Null Column shape: (3, 2)
┌─────┬─────┐
│ a_1 ┆ a_2 │
│ --- ┆ --- │
│ u8  ┆ u8  │
╞═════╪═════╡
│ 1   ┆ 0   │
│ 0   ┆ 1   │
│ 0   ┆ 0   │
└─────┴─────┘

@florian-klein florian-klein changed the title feat: Added drop_nulls option to to_dummies feat: Added drop_nulls option to to_dummies Jun 17, 2025
@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars and removed title needs formatting labels Jun 17, 2025
Copy link

codecov bot commented Jun 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.62%. Comparing base (171c847) to head (edcbc8a).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #23215      +/-   ##
==========================================
+ Coverage   80.61%   80.62%   +0.01%     
==========================================
  Files        1639     1639              
  Lines      220007   220027      +20     
  Branches     2814     2814              
==========================================
+ Hits       177350   177401      +51     
+ Misses      41990    41959      -31     
  Partials      667      667              

☔ View full report in Codecov by Sentry.
📢 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

dummy_null in Series.to_dummies
1 participant