Skip to content

feat: Add dtype to str.to_integer() #22239

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

math-hiyoko
Copy link
Contributor

@math-hiyoko math-hiyoko commented Apr 12, 2025

This adds a dtype option to str.to_integer(), limited to PolarsIntegerTypes. Each dtype corresponds to a call to the appropriate Rust from_str_radix implementation. The default type is pl.Int64.

Example

import polars as pl
df = pl.DataFrame(
    {
        "str": [
            "-6129899454972456276923959272",
            "1A44E53BFEBA967E6682FBB0",
            "10100110111110110101110100000100110010101111000100011000000100010101010101101011111111101000",
            None,
            "7798994549724957734429272",
        ],
        "base": [10, 16, 2, 8, None],
    }
)
df.select(int=pl.col("str").str.to_integer(base="base", dtype=pl.Int128))
# shape: (5, 1)
# Series: 'bin' [i32]
# ┌───────────────────────────────┐
# │ int                           │
# │ ---                           │
# │ i128                          │
# ╞═══════════════════════════════╡
# │ -6129899454972456276923959272 │
# │ 8129899739726392769273592752  │
# │ 3229899454972495776923959272  │
# │ null                          │
# │ null                          │
# └───────────────────────────────┘

close #20567

@math-hiyoko math-hiyoko marked this pull request as draft April 12, 2025 11:01
@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 labels Apr 12, 2025
Copy link

codecov bot commented Apr 12, 2025

Codecov Report

Attention: Patch coverage is 97.93814% with 2 lines in your changes missing coverage. Please review.

Project coverage is 80.77%. Comparing base (f5ada14) to head (3b76f6d).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
.../polars-ops/src/chunked_array/strings/namespace.rs 98.75% 1 Missing ⚠️
.../polars-python/src/lazyframe/visitor/expr_nodes.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #22239   +/-   ##
=======================================
  Coverage   80.77%   80.77%           
=======================================
  Files        1640     1640           
  Lines      235577   235629   +52     
  Branches     2714     2714           
=======================================
+ Hits       190282   190327   +45     
- Misses      44654    44661    +7     
  Partials      641      641           

☔ 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.

@math-hiyoko math-hiyoko marked this pull request as ready for review April 14, 2025 13:24
@math-hiyoko
Copy link
Contributor Author

Everything is ready now

@orlp
Copy link
Member

orlp commented Apr 15, 2025

Also closes #11612.

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.

Parse integer strings to Int128, e.g. str.to_integer(..., dtype=pl.Int128)
2 participants