polars.from_repr#

polars.from_repr(data: str) DataFrame | Series[source]#

Construct a Polars DataFrame or Series from its string representation.

Parameters:
data

A string containing a polars DataFrame or Series repr; does not need to be trimmed of whitespace (or leading prompts) as the repr will be found/extracted automatically.

Notes

This function handles the default UTF8_FULL and UTF8_FULL_CONDENSED DataFrame tables (with or without rounded corners). Truncated columns/rows are omitted, wrapped headers are accounted for, and dtypes automatically identified.

Currently compound/nested dtypes such as List and Struct are not supported; neither are Object dtypes.

Examples

From DataFrame table repr:

>>> df = pl.from_repr(
...     '''
...     Out[3]:
...     shape: (1, 5)
...     ┌───────────┬────────────┬───┬───────┬────────────────────────────────┐
...     │ source_ac ┆ source_cha ┆ … ┆ ident ┆ timestamp                      │
...     │ tor_id    ┆ nnel_id    ┆   ┆ ---   ┆ ---                            │
...     │ ---       ┆ ---        ┆   ┆ str   ┆ datetime[μs, Asia/Tokyo]       │
...     │ i32       ┆ i64        ┆   ┆       ┆                                │
...     ╞═══════════╪════════════╪═══╪═══════╪════════════════════════════════╡
...     │ 123456780 ┆ 9876543210 ┆ … ┆ a:b:c ┆ 2023-03-25 10:56:59.663053 JST │
...     │ …         ┆ …          ┆ … ┆ …     ┆ …                              │
...     │ 803065983 ┆ 2055938745 ┆ … ┆ x:y:z ┆ 2023-03-25 12:38:18.050545 JST │
...     └───────────┴────────────┴───┴───────┴────────────────────────────────┘
... '''
... )
>>> df
shape: (2, 4)
┌─────────────────┬───────────────────┬───────┬────────────────────────────────┐
│ source_actor_id ┆ source_channel_id ┆ ident ┆ timestamp                      │
│ ---             ┆ ---               ┆ ---   ┆ ---                            │
│ i32             ┆ i64               ┆ str   ┆ datetime[μs, Asia/Tokyo]       │
╞═════════════════╪═══════════════════╪═══════╪════════════════════════════════╡
│ 123456780       ┆ 9876543210        ┆ a:b:c ┆ 2023-03-25 10:56:59.663053 JST │
│ 803065983       ┆ 2055938745        ┆ x:y:z ┆ 2023-03-25 12:38:18.050545 JST │
└─────────────────┴───────────────────┴───────┴────────────────────────────────┘

From Series repr:

>>> s = pl.from_repr(
...     '''
...     shape: (3,)
...     Series: 's' [bool]
...     [
...        true
...        false
...        true
...     ]
...     '''
... )
>>> s.to_list()
[True, False, True]