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): Improve Polars Enum dtype init from standard Python enums #19997

Merged
merged 1 commit into from
Nov 28, 2024

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Nov 26, 2024

Extends #19926.

  • Seamless init from all types of string-valued Python Enums. This means that the caller's Enum definition doesn't have to be explicitly declared as SomeEnum(str, Enum) (py < 3.11) or SomeEnum(StrEnum) (py >=3.11), as long as the referenced values are strings.
  • More specific error message on attempt to initialise from unsupported Python enum types (eg: Flag, IntEnum, etc...)

Example

import polars as pl
import enum

class PolarsURL(enum.Enum):
    HOMEPAGE = "https://pola.rs"
    BLOG = "https://pola.rs/posts/"
    USER_GUIDE = "https://docs.pola.rs/"
    PYTHON_API = "https://docs.pola.rs/py-polars/html/reference/"
    RUST_API = "https://docs.rs/polars/latest/polars/"
    SERVICES = "https://pola.rs/our-services/"

Before:

pl.Enum(PolarsURL)
# TypeError: Enum categories must be strings; found data of type Object

After:

pl.Enum(PolarsURL)
# Enum(categories=[
#     'https://pola.rs', 
#     'https://pola.rs/posts/', 
#     'https://docs.pola.rs/', 
#     'https://docs.pola.rs/py-polars/html/reference/', 
#     'https://docs.rs/polars/latest/polars/',
#     'https://pola.rs/our-services/',
# ])

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars labels Nov 26, 2024
@alexander-beedie alexander-beedie added the A-dtype Area: data types in general label Nov 26, 2024
Copy link

codecov bot commented Nov 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 79.50%. Comparing base (0b1d520) to head (32edaea).
Report is 19 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #19997   +/-   ##
=======================================
  Coverage   79.49%   79.50%           
=======================================
  Files        1556     1556           
  Lines      216407   216413    +6     
  Branches     2456     2458    +2     
=======================================
+ Hits       172043   172052    +9     
+ Misses      43806    43803    -3     
  Partials      558      558           

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

@ritchie46 ritchie46 merged commit b83d847 into pola-rs:main Nov 28, 2024
18 checks passed
@alexander-beedie alexander-beedie deleted the python-enum-dtype-init branch November 28, 2024 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-dtype Area: data types in general 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