Serialization & Deserialization
The serde module provides utilities for serializing and deserializing Narwhals dtypes to and from string
representations.
anyschema.serde
deserialize_dtype
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 src/anyschema/serde.py
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
serialize_dtype
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})"
Source code in src/anyschema/serde.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | |