1

Is it possible to do something like the following in python polars:

import polars as pl
import statsmodels.api as sm

lowess = sm.nonparametric.lowess

df = pl.DataFrame([pl.Series('x', ['a', 'a', 'a', 'b','b', 'b']),
    pl.Series('y', [1, 2, 3, 1, 2, 3]),
    pl.Series('z', [.2, .3, .5, .1, .3, .7])]
)

df.with_columns(
    pl.struct('z', 'y').map_batches(lambda cols: pl.DataFrame(lowess(cols['z'], cols['y'], frac = .1)))
      .over('x')
)
# ComputeError: TypeError: cannot select elements using Sequence with elements of type 'str'

I want to group by one or more columns and then apply a function with more than 1 argument.

1 Answer 1

1

To reach specific field of struct you can use .struct.field() method.

def get_lowess(s: pl.Series) -> pl.Series:
    return pl.Series(
        lowess(s.struct.field("z"), s.struct.field("y"), frac = .1)
    )

df.with_columns(
    pl.struct('z', 'y').map_batches(get_lowess).over('x').alias("lowess")
)
shape: (6, 4)
┌─────┬─────┬─────┬───────────────┐
│ x   ┆ y   ┆ z   ┆ lowess        │
│ --- ┆ --- ┆ --- ┆ ---           │
│ str ┆ i64 ┆ f64 ┆ array[f64, 2] │
╞═════╪═════╪═════╪═══════════════╡
│ a   ┆ 1   ┆ 0.2 ┆ [1.0, 0.2]    │
│ a   ┆ 2   ┆ 0.3 ┆ [2.0, 0.3]    │
│ a   ┆ 3   ┆ 0.5 ┆ [3.0, 0.5]    │
│ b   ┆ 1   ┆ 0.1 ┆ [1.0, 0.1]    │
│ b   ┆ 2   ┆ 0.3 ┆ [2.0, 0.3]    │
│ b   ┆ 3   ┆ 0.7 ┆ [3.0, 0.7]    │
└─────┴─────┴─────┴───────────────┘
Sign up to request clarification or add additional context in comments.

7 Comments

Forgot to add pl.Series()
Thanks! that works as I wanted. Is there a way to do this with map? I thought apply is for row-wise operations and not columnar.
It seems that map (in comparison with apply) takes the entire column and over does not affect on it.
@troh "apply works on the smallest logical elements for that operation" is where it is documented if that helps.
@troh .over() is also a groupby context.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.