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): Add top-level nth(n) method, to go with existing first and last #16112

Merged
merged 1 commit into from
May 8, 2024

Conversation

alexander-beedie
Copy link
Collaborator

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

Had a local use-case that would have benefited from this and realised we hadn't exposed the existing Expr::nth (as used by first & last) up to the Python API. Allows for selection of the "nth" col or value, depending on context. Also slightly clarified the existing first/last docstrings.

Example

import polars as pl

df = pl.DataFrame({
    "a": [1, 8, 3],
    "b": [4.5, 5.0, 2.5],
    "c": ["foo", "bar", "baz"],
})
# shape: (3, 3)
# ┌─────┬─────┬─────┐
# │ a   ┆ b   ┆ c   │
# │ --- ┆ --- ┆ --- │
# │ i64 ┆ f64 ┆ str │
# ╞═════╪═════╪═════╡
# │ 1   ┆ 4.5 ┆ foo │
# │ 8   ┆ 5.0 ┆ bar │
# │ 3   ┆ 2.5 ┆ baz │
# └─────┴─────┴─────┘
df.select(pl.nth(1))
# shape: (3, 1)
# ┌─────┐
# │ b   │
# │ --- │
# │ f64 │
# ╞═════╡
# │ 4.5 │
# │ 5.0 │
# │ 2.5 │
# └─────┘
df.select(pl.nth(1,"c","b"))
# shape: (1, 2)
# ┌─────┬─────┐
# │ c   ┆ b   │
# │ --- ┆ --- │
# │ str ┆ f64 │
# ╞═════╪═════╡
# │ bar ┆ 5.0 │
# └─────┴─────┘

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

codecov bot commented May 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.97%. Comparing base (acb601d) to head (29165e4).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #16112   +/-   ##
=======================================
  Coverage   80.97%   80.97%           
=======================================
  Files        1386     1386           
  Lines      178479   178491   +12     
  Branches     2877     2878    +1     
=======================================
+ Hits       144530   144541   +11     
  Misses      33458    33458           
- Partials      491      492    +1     

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

Copy link
Member

@ritchie46 ritchie46 left a comment

Choose a reason for hiding this comment

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

Funny, didn't realize this was catching dust.

@ritchie46 ritchie46 merged commit ddc30ab into pola-rs:main May 8, 2024
28 checks passed
@alexander-beedie alexander-beedie deleted the nth-func branch May 8, 2024 11:09
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.

None yet

2 participants