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(rust): guarantee schema-stable col(dtype) selection #6674

Merged

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Feb 4, 2023

Issue

  • Using dtype-based column selection with more than one dtype (eg: polars.datatypes.NUMERIC_DTYPES) was not guaranteed to be stable between sessions/platforms/etc, as those dtype groups are set-based.
  • Other (non-set) multi-dtype selections return columns in dtype-order, not schema-order.

Solution

Switch the inner/outer filter-order in expand_dtypes when determining which columns match.
Guarantees stable column selection while maintaining the underlying schema-order.

Example

from polars.datatypes import NUMERIC_DTYPES
import polars as pl

df = pl.DataFrame(
    data=[],
    schema={
        "a": pl.Int8,
        "b": pl.Int16,
        "c": pl.Int32,
        "d": pl.Int64,
        "e": pl.Float32,
        "f": pl.Float64,
        "g": pl.UInt8,
        "h": pl.UInt16,
        "i": pl.UInt32,
        "j": pl.UInt64,
    },
)

Before: (unstable column order - changes between sessions)

df.select( pl.col(NUMERIC_DTYPES) )
# ┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐
# │ f   ┆ c   ┆ e   ┆ b   ┆ j   ┆ a   ┆ i   ┆ h   ┆ g   ┆ d   │
# │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
# │ f64 ┆ i32 ┆ f32 ┆ i16 ┆ u64 ┆ i8  ┆ u32 ┆ u16 ┆ u8  ┆ i64 │
# ╞═════╪═════╪═════╪═════╪═════╪═════╪═════╪═════╪═════╪═════╡
# └─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘

After: (stable, and schema-ordered)

df.select( pl.col(NUMERIC_DTYPES) )
# ┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐
# │ a   ┆ b   ┆ c   ┆ d   ┆ e   ┆ f   ┆ g   ┆ h   ┆ i   ┆ j   │
# │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
# │ i8  ┆ i16 ┆ i32 ┆ i64 ┆ f32 ┆ f64 ┆ u8  ┆ u16 ┆ u32 ┆ u64 │
# ╞═════╪═════╪═════╪═════╪═════╪═════╪═════╪═════╪═════╪═════╡
# └─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘

Note

Selection in dtype order remains trivial (and each set of matching cols is also now stable), eg:

df.select( [pl.col(FLOAT_DTYPES), pl.col(INTEGER_DTYPES), ...] )

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature rust Related to Rust Polars labels Feb 4, 2023
@alexander-beedie alexander-beedie force-pushed the stable-dtype-col-selection branch 3 times, most recently from de59491 to 718d3d0 Compare February 4, 2023 19:30
@ritchie46 ritchie46 merged commit b736cdf into pola-rs:master Feb 5, 2023
@alexander-beedie alexander-beedie deleted the stable-dtype-col-selection branch February 5, 2023 15:34
Vincenthays pushed a commit to Vincenthays/polars that referenced this pull request Feb 9, 2023
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 rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants