pandantic
pandantic ¶
Defines types for Pandas Series and DataFrame serialization and deserialization.
PandasDataFrame
module-attribute
¶
PandasDataFrame = Annotated[
DataFrame, BeforeValidator(to_dataframe), WrapSerializer(from_dataframe)
]
Type alias for Pandas DataFrame objects that can be serialized and deserialized.
This type alias is used to define the expected data structure when working with Pandas DataFrame objects in a way that allows them to be both serialized and deserialized using the DataFrameSerDe
class.
The PandasDataFrame
type alias is defined as an Annotated type, which means it has two type arguments: pd.DataFrame
, which specifies the expected data structure for this type, and a BeforeValidator and PlainSerializer, respectively.
This allows you to use Pandas DataFrame objects in your code while ensuring that they can be serialized and deserialized using the DataFrameSerDe
class.
PandasSeries
module-attribute
¶
PandasSeries = Annotated[Series, BeforeValidator(to_series), WrapSerializer(from_series)]
Type alias for Pandas Series objects that can be serialized and deserialized.
This type alias is used to define the expected data structure when working with Pandas Series objects in a way that allows them to be both serialized and deserialized using the SeriesSerDe
class.
The PandasSeries
type alias is defined as an Annotated type, which means it has two type arguments: pd.Series
, which specifies the expected data structure for this type, and a BeforeValidator and PlainSerializer, respectively.
This allows you to use Pandas Series objects in your code while ensuring that they can be serialized and deserialized using the SeriesSerDe
class.
DataFrameSerDe ¶
Bases: BaseModel
Class to serialize and deserialize Pandas DataFrame objects.
Attributes:
Name | Type | Description |
---|---|---|
columns |
NDArray[Shape['*'], Any]
|
Column names of the dataframe |
values |
NDArray[Shape['*, *'], Any]
|
Values of the dataframe |
index |
NDArray[Shape['*'], Any]
|
Index of the dataframe |
index_name |
int | str | None
|
Name of the index |
columns_name |
list[int | str | None]
|
Names of the columns |
Methods: as_dataframe(): Convert to Pandas DataFrame object from_dict(data: dict) -> DataFrameSerDe: Create a new instance from a dictionary model_validate_json(json: str | bytes, loc: Any = None, prop: Any = None, cls: Any = None, **kwargs: Any) -> Self: Validate and deserialize JSON data into the model.
as_dataframe ¶
Convert to Pandas DataFrame object.
Returns:
Type | Description |
---|---|
DataFrame
|
Pandas DataFrame object from stored data |
Source code in src/data_handlers/custom_serde_definitions/pandantic.py
from_dataframe
classmethod
¶
from_dataframe(df: DataFrame, nxt: SerializerFunctionWrapHandler, info: FieldSerializationInfo)
Create a new instance from a Pandas DataFrame object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
Pandas DataFrame object to convert. |
required |
Returns:
Type | Description |
---|---|
cls
|
New instance created from the Pandas DataFrame object. |
Source code in src/data_handlers/custom_serde_definitions/pandantic.py
from_dict
classmethod
¶
from_dict(d: dict)
Create a new instance from a dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d
|
dict
|
Dictionary containing the data. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
New DataFrame created from the dictionary. |
Source code in src/data_handlers/custom_serde_definitions/pandantic.py
to_dataframe
classmethod
¶
Convert input to a Pandas DataFrame object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input
|
DataFrame | dict | str
|
Input data to convert. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
Pandas DataFrame object from input data. |
Source code in src/data_handlers/custom_serde_definitions/pandantic.py
SeriesSerDe ¶
Bases: BaseModel
Class to serialize and deserialize Pandas Series objects.
Attributes:
Name | Type | Description |
---|---|---|
name |
int | str | None
|
Name of the series |
values |
NDArray[Shape['*'], Any]
|
Values of the series |
index |
NDArray[Shape['*'], Any]
|
Index of the series |
index_name |
int | str | None
|
Name of the index |
Methods: as_series(): Convert to Pandas Series object from_dict(data: dict) -> SeriesSerDe: Create a new instance from a dictionary model_validate_json(json: str | bytes, loc: Any = None, prop: Any = None, cls: Any = None, **kwargs: Any) -> Self: Validate and deserialize JSON data into the model.
as_series ¶
Convert to Pandas Series object.
Returns:
Type | Description |
---|---|
Series
|
Pandas Series object from stored data |
Source code in src/data_handlers/custom_serde_definitions/pandantic.py
from_dict
classmethod
¶
from_dict(d: dict)
Create a new instance from a dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d
|
dict
|
Dictionary containing the data. |
required |
Returns:
Type | Description |
---|---|
cls
|
New instance created from the dictionary. |
Source code in src/data_handlers/custom_serde_definitions/pandantic.py
from_series
classmethod
¶
from_series(series: Series, nxt: SerializerFunctionWrapHandler, info: FieldSerializationInfo)
Create a new instance from a Pandas Series object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
series
|
Series
|
Pandas Series object to convert. |
required |
Returns:
Type | Description |
---|---|
cls
|
New instance created from the Pandas Series object. |
Source code in src/data_handlers/custom_serde_definitions/pandantic.py
to_series
classmethod
¶
Convert input to a Pandas Series object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input
|
Series | dict | str
|
Input data to convert. |
required |
Returns:
Type | Description |
---|---|
Series
|
Pandas Series object from input data. |