Skip to content
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

feat(python): Allow use of Python types in cs.by_dtype and col #20491

Merged
merged 1 commit into from
Dec 29, 2024

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Dec 29, 2024

Consistency/ergonomics; we accept Python types in most places already (eg: Schema, schema_overrides, etc), but were missing integration in the cs.by_dtype selector and col. This adds support for both.

Note

Selecting Python types that map to multiple Polars dtypes, such as int, float, datetime, or timedelta will select all the related Polars dtypes (eg: matching on int is equivalent to matching on all signed/unsigned Polars integer dtypes).

Example

from datetime import datetime
import polars.selectors as cs
import polars as pl

df = pl.DataFrame(
    {
        "idx": [],
        "dt1": [],
        "dt2": [],
    },
    schema_overrides={
        "idx": pl.UInt32,
        "dt1": pl.Datetime("ms"),
        "dt2": pl.Datetime(time_zone="Asia/Tokyo"),
    },
)
df.select(cs.by_dtype(datetime))
# shape: (0, 2)
# ┌──────────────┬──────────────────────────┐
# │ dt1          ┆ dt2                      │
# │ ---          ┆ ---                      │
# │ datetime[ms] ┆ datetime[μs, Asia/Tokyo] │
# ╞══════════════╪══════════════════════════╡
# └──────────────┴──────────────────────────┘
df.select(pl.col(int))
# shape: (0, 1)
# ┌─────┐
# │ idx │
# │ --- │
# │ u32 │
# ╞═════╡
# └─────┘

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars labels Dec 29, 2024
Copy link

codecov bot commented Dec 29, 2024

Codecov Report

Attention: Patch coverage is 86.11111% with 5 lines in your changes missing coverage. Please review.

Project coverage is 79.01%. Comparing base (f43a7d4) to head (e0df706).
Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
py-polars/polars/functions/col.py 87.87% 2 Missing and 2 partials ⚠️
py-polars/polars/selectors.py 66.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #20491      +/-   ##
==========================================
- Coverage   79.02%   79.01%   -0.01%     
==========================================
  Files        1563     1563              
  Lines      220596   220626      +30     
  Branches     2492     2502      +10     
==========================================
+ Hits       174317   174334      +17     
- Misses      45706    45718      +12     
- Partials      573      574       +1     

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

@ritchie46 ritchie46 merged commit ef32c9a into pola-rs:main Dec 29, 2024
29 checks passed
@alexander-beedie alexander-beedie deleted the dtype-col-selectors branch December 29, 2024 18:22
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants