3

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?

irohamca
  • 497
  • 3
  • 19

1 Answers1

6

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_column(
    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: https://pola-rs.github.io/polars-book/user-guide/dsl/intro.html

ritchie46
  • 10,405
  • 1
  • 24
  • 43