irohamca
irohamca

Reputation: 507

Python Polars column based update does not work

I use Polars library for dataframe manipulations. I have two dataframes, I want to update column values of one dataframe with single value which is got from another dataframe based on a condition. This is the code:

tmp = df[df['UnifiedInvoiceID'] == inv]
mask = (df_invoice_features['UnifiedInvoiceID'] == inv)
df_invoice_features[mask, 'UnifiedCustomerID'] = tmp[0, 'UnifiedCustomerID']

And, this is the error:

PySeries.new_u64() missing 1 required positional argument: '_strict'

Why do you think such an error returns?

Upvotes: 3

Views: 2074

Answers (1)

ritchie46
ritchie46

Reputation: 14710

Polars syntax is very different than that of pandas. It seems to me you are trying to modify values like you would do on a pandas DataFrame.

Here is an example of how you set column values:

df = pl.DataFrame({
    "a": [1, 2, 3, 4, 5],
    "b": list("abcde")
})

df.with_columns(
    pl.when(pl.col("a") > 3).then(10).otherwise(pl.col("a")).alias("new")
)

Ouputs:

shape: (5, 3)
┌─────┬─────┬─────┐
│ a   ┆ b   ┆ new │
│ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ i64 │
╞═════╪═════╪═════╡
│ 1   ┆ a   ┆ 1   │
│ 2   ┆ b   ┆ 2   │
│ 3   ┆ c   ┆ 3   │
│ 4   ┆ d   ┆ 10  │
│ 5   ┆ e   ┆ 10  │
└─────┴─────┴─────┘

If you give a small example I can give a more thorough example. I'd also recommend reading the user guide, especially the expression guide:

Upvotes: 7

Related Questions