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: Allow designation of a custom name for the value_counts "count" column #16434

Merged
merged 2 commits into from
May 23, 2024

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented May 23, 2024

Closes #16425.

While we don't usually (but do sometimes #15222), I think we should allow a "name" parameter here, because otherwise we are making the caller change their column names (in the case of a clash) to account for us forcing one called "count" onto them.

It is more user-friendly to allow our "count" column to be renamed, as there is no way for the caller to do so, and working around it with pre/post aliasing to maintain their original column name is pretty ugly (and verges on painful in the DataFrame case) 🤔

Example

Series

s = pl.Series("count", [20, 20, 60, 50, 50, 50])

With a "name" param:

s.value_counts(sort=True, name="n")
# shape: (3, 2)
# ┌───────┬─────┐
# │ count ┆ n   │
# │ ---   ┆ --- │
# │ i64   ┆ u32 │
# ╞═══════╪═════╡
# │ 50    ┆ 3   │
# │ 20    ┆ 2   │
# │ 60    ┆ 1   │
# └───────┴─────┘

Without a "name" param achieving the same output is... not ergonomic :)

s.alias("temp_count").value_counts(sort=True).alias(
    {"count":"n", "temp_count":"count"}
)
# shape: (3, 2)
# ┌───────┬─────┐
# │ count ┆ n   │
# │ ---   ┆ --- │
# │ i64   ┆ u32 │
# ╞═══════╪═════╡
# │ 50    ┆ 3   │
# │ 20    ┆ 2   │
# │ 60    ┆ 1   │
# └───────┴─────┘

DataFrame

And it's even worse for DataFrame...

df = pl.DataFrame({"count": [20, 20, 60, 50, 50, 50]})

With a "name" param:

df.select(pl.col("count").value_counts(name="n"))
# shape: (3, 1)
# ┌───────────┐
# │ count     │
# │ ---       │
# │ struct[2] │
# ╞═══════════╡
# │ {60,1}    │
# │ {50,3}    │
# │ {20,2}    │
# └───────────┘
df.unnest("count")
# shape: (3, 2)
# ┌───────┬─────┐
# │ count ┆ n   │
# │ ---   ┆ --- │
# │ i64   ┆ u32 │
# ╞═══════╪═════╡
# │ 50    ┆ 3   │
# │ 60    ┆ 1   │
# │ 20    ┆ 2   │
# └───────┴─────┘

Without a "name" param we have to tie ourselves in knots to achieve the same output, first aliasing the target column and then renaming both the resulting struct column and its component fields:

df.select(
    pl.col("count")
    .alias("temp_count")
    .value_counts()
    .struct.rename_fields(["count", "n"])
).rename({"temp_count": "count"})

@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 May 23, 2024
Copy link
Member

@stinodego stinodego left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, looks good to me. Thanks for picking this up!

@alexander-beedie
Copy link
Collaborator Author

Yep, looks good to me. Thanks for picking this up!

It was an ideal coffee-sized patch; that's my fundamental unit of work these days 😆

@alexander-beedie alexander-beedie merged commit d5f9c3b into pola-rs:main May 23, 2024
30 checks passed
@alexander-beedie alexander-beedie deleted the value-counts-name branch May 24, 2024 05:51
Wouittone pushed a commit to Wouittone/polars that referenced this pull request Jun 22, 2024
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.

Add argument to Series.value_counts to set the name of the new column created
2 participants