typeddfs._mixins._dataclass_mixin
ο
Dataclass mixin.
Module Contentsο
- class typeddfs._mixins._dataclass_mixin.TypedDfDataclassο
Just a
dataclass
for TypedDfs. Containsget_df_type()
to point to the original DataFrame.- get_as_dict(self) Mapping[str, Any] ο
Returns a mapping from the dataclass field name to the value.
- classmethod get_fields(cls) Sequence[dataclasses.Field] ο
Returns the fields of this dataclass.
- class typeddfs._mixins._dataclass_mixin._DataclassMixinο
- classmethod _create_dataclass(cls, fields: Sequence[Tuple[str, Type[Any]]]) Type[TypedDfDataclass] ο
- classmethod create_dataclass(cls, reserved: bool = True) Type[TypedDfDataclass] ο
Creates a best-effort immutable
dataclass
for this type. The fields will depend on the columns and index levels present inget_typing()
. The type of each field will correspond to the specified dtype (typeddfs.df_typing.DfTyping.auto_dtypes()
), falling back toAny
if none is specified.Note
If this type can support additional columns (
typeddfs.df_typing.DfTyping.is_strict()
is the default,False
), the dataclass will not be able to support extra fields. For most cases,typeddfs.abs_dfs.AbsDf.to_dataclass_instances()
is better.- Parameters
reserved β Include reserved columns and index levels
- Returns
A subclass of
typeddfs.abs_dfs.TypedDfDataclass
- classmethod from_dataclass_instances(cls, instances: Sequence[TypedDfDataclass]) __qualname__ ο
Creates a new instance of this DataFrame type from dataclass instances. This mostly delegates to
pd.DataFrame.__init__
, callingcls.of(instances)
. It is provided for consistency withto_dataclass_instances()
.- Parameters
instances β A sequence of dataclass instances. Although typed as
typeddfs.abs_dfs.TypedDfDataclass
, any type created by Pythonβsdataclass
module should work.- Returns
A new instance of this type
- to_dataclass_instances(self) Sequence[TypedDfDataclass] ο
Creates a dataclass from this DataFrame and returns instances. Also see
from_dataclass_instances()
.Note
Dataclass elements are equal if fields and values match, even if they are of different types. This was done by overriding
__eq__
to enable comparing results from separate calls to this method. Specifically,typeddfs.abs_dfs.TypedDfDataclass.get_as_dict()
must return True.Caution
Fields cannot be included if columns are not present. If
self.get_typing().is_strict is False
, then the dataclass created by two different DataFrames of typeself.__class__
may have different fields.Caution
A new dataclass is created per call, so
df.to_dataclass_instances()[0] is not df.to_dataclass_instances()[0]
.