3

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?

Dimitrius
  • 564
  • 6
  • 21

1 Answers1

7

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        │
└─────┴─────────────┴─────────────┘
glebcom
  • 1,131
  • 5
  • 14
  • This is a good answer, but you have to know lengths on the moment of construction, so you cannot use it just in expression, or for a LazyFrame. – Dimitrius Sep 16 '22 at 12:28
  • 1
    Though you can do in like this without length! You can edit your answer and I'll mark it as correct answer. a.with_columns([pl.lit(pl.Series('b', [[]], dtype=pl.List))]) – Dimitrius Sep 16 '22 at 12:35
  • Yesss) I removed length. – glebcom Sep 16 '22 at 12:57