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

2

8

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?

Son answered 16/9, 2022 at 10:28 Comment(0)
B
11

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        │
└─────┴─────────────┴─────────────┘
Brainstorm answered 16/9, 2022 at 11:59 Comment(3)
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.Son
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))])Son
Yesss) I removed length.Brainstorm
O
3

you could do this:

a.with_columns(b=pl.lit([]))

shape: (3, 2)
┌─────┬────────────┐
│ a   ┆ b          │
│ --- ┆ ---        │
│ i64 ┆ list[null] │
╞═════╪════════════╡
│ 1   ┆ []         │
│ 2   ┆ []         │
│ 3   ┆ []         │
└─────┴────────────┘
Openhearth answered 30/5, 2024 at 19:34 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.