mt.pandas.dataclass
Functions dealing with dataclass columns.
Functions
pack_dataclass()
: Packs a list of component fields of a dataframe as a dataclass field.unpack_dataclass()
: Unpacks a dataclass field of a dataframe as a list of component fields.
- mt.pandas.dataclass.pack_dataclass(df, output_col: str, klass, field_map: dict | None = None, drop: bool = False)
Packs a list of component fields of a dataframe as a dataclass field.
- Parameters:
df (pandas.DataFrame) – the input dataframe to work on
output_col (str) – the name of the output dataclass field
klass (object) – the dtype of the output dataclass field
field_map (dict, optional) – if provided, a mapping that maps each component field’s name into a field of the dataclass
drop (bool) – whether or not (default) to drop the component fields in the dataframe
- Returns:
the output dataframe, where the dataclass field is appended to the input dataframe, and optionally the component fields dropped
- Return type:
pandas.DataFrame
- mt.pandas.dataclass.unpack_dataclass(df, input_col: str, klass: object | None = None, field_map: dict | None = None, drop: bool = False)
Unpacks a dataclass field of a dataframe as a list of component fields.
- Parameters:
df (pandas.DataFrame) – the input dataframe to work on
input_col (str) – the name of the input dataclass field
klass (object, optional) – the dtype of the input dataclass field. To be detected if not provided.
field_map (dict, optional) – if provided, a mapping that maps each component field’s name into a field of the dataclass
drop (bool) – whether or not (default) to drop the dataclass field after unpacking
- Returns:
the output dataframe, where new component fields are generated, and optionally the dataclass field dropped
- Return type:
pandas.DataFrame