1

I have a polars dataframe that I want to group by and concatenate the unique values in as a single entry.

in pandas, I go:

def unique_colun_values(x):
    return('|'.join(set(x)))

dd=pd.DataFrame({'col1':[1,1,2,3,4],'col2':['a','a','a','b','b'],'col3':['qwe','rty','asd','fgh','zxc']})
dd.groupby('col1').agg(lambda x: unique_colun_values(x))

This works fine

when I try to implement in polars:

pl.from_pandas(dd).group_by('col1').agg(lambda x: unique_colun_values(x), allow_object=True)

I get the following error:

TypeError: cannot create expression literal for value of type function.

Hint: Pass `allow_object=True` to accept any value and create a literal of type Object.

Am I missing something?

1 Answer 1

5

According to the documentation, unlike pandas .agg, polars .agg method does not accept a function as its first parameter, it only accepts polars expressions. This is what the error tells you, it fails when trying to convert the function into a polars expression.

The solution is to write unique_colun_values as an expression.

import pandas as pd
import polars as pl

dd = pd.DataFrame(
    {
        "col1": [1, 1, 2, 3, 4],
        "col2": ["a", "a", "a", "b", "b"],
        "col3": ["qwe", "rty", "asd", "fgh", "zxc"],
    }
)

res = pl.from_pandas(dd).group_by('col1').agg(pl.all().unique().str.join("|"))

print(res)

Output:

┌──────┬──────┬─────────┐
│ col1 ┆ col2 ┆ col3    │
│ ---  ┆ ---  ┆ ---     │
│ i64  ┆ str  ┆ str     │
╞══════╪══════╪═════════╡
│ 3    ┆ b    ┆ fgh     │
│ 2    ┆ a    ┆ asd     │
│ 4    ┆ b    ┆ zxc     │
│ 1    ┆ a    ┆ qwe|rty │
└──────┴──────┴─────────┘

If you prefer, you can still write the operation as a function that returns an expression.

def unique_colun_values(col: pl.Expr) -> pl.Expr:
    return col.unique().str.join("|")

# You can call the function using
res = pl.from_pandas(dd).group_by("col1").agg(unique_colun_values(pl.all()))

# Or, using pl.Expr.pipe 
res = pl.from_pandas(dd).group_by("col1").agg(pl.all().pipe(unique_colun_values))
Sign up to request clarification or add additional context in comments.

Comments

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.