Serialization & Deserialization¶
The serde module provides utilities for serializing and deserializing Narwhals dtypes to and from string
representations.
API Reference¶
anyschema.serde
¶
deserialize_dtype(into_dtype: str) -> DType
¶
Deserialize a string representation of a Narwhals dtype back to the dtype object.
Handles both simple and complex nested types using regex and recursion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
into_dtype
|
str
|
String representation of the dtype (e.g., "Int64", "List(String)", "Struct({'a': Int64, 'b': List(String)})") |
required |
Returns:
| Type | Description |
|---|---|
DType
|
The corresponding Narwhals DType object |
Examples:
>>> deserialize_dtype("Int64")
Int64
>>> deserialize_dtype("List(String)")
List(String)
>>> deserialize_dtype("Datetime(time_unit='ms', time_zone='UTC')")
Datetime(time_unit='ms', time_zone='UTC')
Source code in anyschema/serde.py
serialize_dtype(dtype: DType) -> str
¶
Serialize a Narwhals dtype to its string representation.
Converts a Narwhals dtype object into a string that can be stored or transmitted
and later reconstructed using deserialize_dtype. The serialization is based on
the dtype's string representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dtype
|
DType
|
A Narwhals DType object to serialize |
required |
Returns:
| Type | Description |
|---|---|
str
|
String representation of the dtype (e.g., "Int64", "List(String)", "Struct({'a': Int64, 'b': String})") |
Examples:
>>> serialize_dtype(Int64())
'Int64'
>>> serialize_dtype(List(String()))
'List(String)'
>>> serialize_dtype(Datetime(time_unit="ms", time_zone="UTC"))
"Datetime(time_unit='ms', time_zone='UTC')"
>>> serialize_dtype(Struct({"a": Int64(), "b": String()}))
"Struct({'a': Int64, 'b': String})"