When Polars assigns the pl.Object type it essentially means: "I do not understand what this is."

By the time you end up with this type, it is generally too late to do anything useful with it.

In this particular case, numpy.random.choice is creating a numpy array of dtype=object

>>> rng.choice([None, "foo"], 3)
array([None, None, 'foo'], dtype=object)

Polars has native .sample() functionality which you could use to create your data instead.

df = pl.select(date = 
    pl.Series([None, "03.04.1998", "03.05.1834", "05.06.2025"])
      .sample(100, with_replacement=True)
)

# shape: (100, 1)
# โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
# โ”‚ date       โ”‚
# โ”‚ ---        โ”‚
# โ”‚ str        โ”‚
# โ•žโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ก
# โ”‚ null       โ”‚
# โ”‚ 05.06.2025 โ”‚
# โ”‚ 03.05.1834 โ”‚
# โ”‚ 03.04.1998 โ”‚
# โ”‚ โ€ฆ          โ”‚
# โ”‚ null       โ”‚
# โ”‚ 03.04.1998 โ”‚
# โ”‚ 03.05.1834 โ”‚
# โ”‚ 03.04.1998 โ”‚
# โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
Answer from jqurious on Stack Overflow
๐ŸŒ
Polars
docs.pola.rs โ€บ user-guide โ€บ expressions โ€บ casting
Casting - Polars user guide
The function cast includes a parameter strict that determines how Polars behaves when it encounters a value that cannot be converted from the source data type to the target data type. The default behaviour is strict=True, which means that Polars will thrown an error to notify the user of the ...
๐ŸŒ
Polars
docs.pola.rs โ€บ api โ€บ python โ€บ dev โ€บ reference โ€บ dataframe โ€บ api โ€บ polars.DataFrame.cast.html
polars.DataFrame.cast โ€” Polars documentation
>>> import polars.selectors as cs >>> df.cast({cs.numeric(): pl.UInt32, cs.temporal(): pl.String}) shape: (3, 3) โ”Œโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ foo โ”† bar โ”† ham โ”‚ โ”‚ --- โ”† --- โ”† --- โ”‚ โ”‚ u32 โ”† u32 โ”† str โ”‚ โ•žโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ก โ”‚ 1 โ”† 6 โ”† 2020-01-02 โ”‚ โ”‚ 2 โ”† 7 โ”† 2021-03-04 โ”‚ โ”‚ 3 โ”† 8 โ”† 2022-05-06 โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ polars โ€บ convert polars cast int to string
Convert Polars Cast Int to String - Spark By {Examples}
February 17, 2025 - In Polars, you can use the cast() function to convert an integer column to a string (Utf8). This is helpful when you need to transform numeric data into
๐ŸŒ
Polars
docs.pola.rs โ€บ docs โ€บ python โ€บ dev โ€บ reference โ€บ expressions โ€บ api โ€บ polars.Expr.str.to_date.html
polars.Expr.str.to_date โ€” Polars documentation
Convert a String column into a Date column ยท Format to use for conversion. Refer to the chrono crate documentation for the full specification. Example: "%Y-%m-%d". If set to None (default), the format is inferred from the data
๐ŸŒ
Polars
docs.pola.rs โ€บ py-polars โ€บ html โ€บ reference โ€บ series โ€บ api โ€บ polars.Series.cast.html
polars.Series.cast โ€” Polars documentation
Series.cast( dtype: PolarsDataType | type[int] | type[float] | type[str] | type[bool], *, strict: bool = True, ) โ†’ Self[source]# Cast between data types. Parameters: dtype ยท DataType to cast to. strict ยท Throw an error if a cast could not be done (for instance, due to an overflow).
Top answer
1 of 4
2

When Polars assigns the pl.Object type it essentially means: "I do not understand what this is."

By the time you end up with this type, it is generally too late to do anything useful with it.

In this particular case, numpy.random.choice is creating a numpy array of dtype=object

>>> rng.choice([None, "foo"], 3)
array([None, None, 'foo'], dtype=object)

Polars has native .sample() functionality which you could use to create your data instead.

df = pl.select(date = 
    pl.Series([None, "03.04.1998", "03.05.1834", "05.06.2025"])
      .sample(100, with_replacement=True)
)

# shape: (100, 1)
# โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
# โ”‚ date       โ”‚
# โ”‚ ---        โ”‚
# โ”‚ str        โ”‚
# โ•žโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ก
# โ”‚ null       โ”‚
# โ”‚ 05.06.2025 โ”‚
# โ”‚ 03.05.1834 โ”‚
# โ”‚ 03.04.1998 โ”‚
# โ”‚ โ€ฆ          โ”‚
# โ”‚ null       โ”‚
# โ”‚ 03.04.1998 โ”‚
# โ”‚ 03.05.1834 โ”‚
# โ”‚ 03.04.1998 โ”‚
# โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
2 of 4
1

Looks like a bug in Polars, could you report it to their GitHub please?

For now you can use tolist

In [24]: import numpy as np
    ...: import polars as pl
    ...:
    ...: rng = np.random.default_rng(12345)
    ...: df = pl.LazyFrame(
    ...:     data={
    ...:         "date": rng.choice(
    ...:             [None, "03.04.1998", "03.05.1834", "05.06.2025"], 100
    ...:         ).tolist(),
    ...:     },
    ...: )

In [25]: df
Out[25]: <LazyFrame [1 col, {"date": Utf8}] at 0x7F6C4A588580>

In [26]: df.collect()
Out[26]:
shape: (100, 1)
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ date       โ”‚
โ”‚ ---        โ”‚
โ”‚ str        โ”‚
โ•žโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ก
โ”‚ 03.05.1834 โ”‚
โ”‚ null       โ”‚
โ”‚ 05.06.2025 โ”‚
โ”‚ 03.04.1998 โ”‚
โ”‚ โ€ฆ          โ”‚
โ”‚ null       โ”‚
โ”‚ null       โ”‚
โ”‚ null       โ”‚
โ”‚ 03.05.1834 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
๐ŸŒ
Code Crew Careers
codecrewcareers.com โ€บ how tos โ€บ how to use the polars cast function
How to Use the Polars Cast Function
February 3, 2025 - The Polars Cast Function lets you change column data types on a Polars DataFrame. Learn how to leverage this functionality!
Find elsewhere
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ polars โ€บ polars cast multiple columns
Polars Cast Multiple Columns - Spark By {Examples}
February 18, 2025 - In Polars, you can cast multiple columns to different data types by using the select() or with_columns() method along with the pl.col() expression and the
๐ŸŒ
Polars
docs.pola.rs โ€บ api โ€บ python โ€บ dev โ€บ reference โ€บ api โ€บ polars.from_dicts.html
polars.from_dicts โ€” Polars documentation
Throw an error if any data value does not exactly match the given or inferred data type for that column. If set to False, values that do not match the data type are cast to that data type or, if casting is not possible, set to null instead.
๐ŸŒ
Scribd
scribd.com โ€บ document โ€บ 828864204 โ€บ Jpegd-Gr12-Memo
Jpegd Gr12 Memo | PDF
Jpegd Gr12 Memo - Free download as PDF File (.pdf) or view presentation slides online.
Rating: 4.8 โ€‹ - โ€‹ 17 votes
Top answer
1 of 1
7

You can use the datatype pl.List(pl.String)

df.with_columns(pl.col("foo").cast(pl.List(pl.String)))
shape: (1, 2)
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ foo             | bar         โ”‚
โ”‚ ---             | ---         โ”‚
โ”‚ list[str]       | str         โ”‚
โ•žโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ก
โ”‚ ["1", "2", "3"] | Hello World โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

To create an actual string - perhaps:

df.with_columns("[" + 
   pl.col("foo").cast(pl.List(pl.String)).list.join(", ") 
   + "]"
)

There's also pl.format()

df.with_columns(
   pl.format("[{}]",
      pl.col("foo").cast(pl.List(pl.String)).list.join(", ")))
shape: (1, 3)
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ foo       | bar         | literal   โ”‚
โ”‚ ---       | ---         | ---       โ”‚
โ”‚ list[i64] | str         | str       โ”‚
โ•žโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ก
โ”‚ [1, 2, 3] | Hello World | [1, 2, 3] โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
๐ŸŒ
Nushell
nushell.sh โ€บ commands โ€บ docs โ€บ polars_cast.html
polars cast | Nushell
Cast a column to a different dtype. > polars cast {flags} (dtype) (column) dtype: The dtype to cast the column to. column: The column to cast. Required when used with a dataframe. Cast a column in a dataframe to a different dtype ยท > [[a b]; [1 2] [3 4]] | polars into-df | polars cast u8 a | polars schema โ•ญโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ•ฎ โ”‚ a โ”‚ u8 โ”‚ โ”‚ b โ”‚ i64 โ”‚ โ•ฐโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ•ฏ ยท
๐ŸŒ
Rust
docs.rs โ€บ polars โ€บ latest โ€บ polars โ€บ prelude โ€บ fn.cast.html
cast in polars::prelude - Rust
polars:: prelude ยท Source ยท pub fn cast(expr: Expr, dtype: impl Into<DataTypeExpr>) -> Expr ยท Available on crate feature lazy only. Expand description ยท Casts the column given by Expr to a different type. Follows the rules of Rust casting, with the exception that integers and floats can ...