How can I add a column of empty arrays to polars.DataFrame?

Question:

I am trying to add a column of empty lists to a polars dataframe in python.

My code

import polars as pl
a = pl.DataFrame({'a': [1, 2, 3]})
a.with_columns([pl.lit([]).alias('b')])

throws

Traceback (most recent call last):
  File "<input>", line 1, in <module>
    a.with_columns([pl.lit([]).alias('b')])
  File "/usr/local/lib/python3.10/site-packages/polars/internals/lazy_functions.py", line 767, in lit
    return pli.wrap_expr(pylit(item, allow_object))
ValueError: could not convert value '[]' as a Literal

How can I create this column?

Asked By: Dimitrius

||

Answers:

This works for me. I wrote pl.Series() with empty lists [] as values:

import polars as pl
from polars import col

df = pl.DataFrame({'a': [1, 2, 3]}) # .lazy()

df = df.with_columns([
    col('a'),
    pl.Series('empty lists', [[]], dtype=pl.List),
    pl.lit(None).alias('null column'),
])
print(df) # print(df.collect()) (in case of LazyFrame)


┌─────┬─────────────┬─────────────┐
│ a   ┆ empty lists ┆ null column │
│ --- ┆ ---         ┆ ---         │
│ i64 ┆ list[f64]   ┆ bool        │
╞═════╪═════════════╪═════════════╡
│ 1   ┆ []          ┆ null        │
├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2   ┆ []          ┆ null        │
├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 3   ┆ []          ┆ null        │
└─────┴─────────────┴─────────────┘
Answered By: glebcom