pandas_dataclasses package#
- class As[source]#
Bases:
Generic
[TPandas
]Pandas data creation by a classmethod (
new
).- new#
Class property decorator dedicated to
As.new
.
- class Spec(name: str | None = None, origin: type | None = None, factory: Callable[[...], DataFrame | Series[Any]] | None = None, fields: Fields = ())[source]#
Bases:
object
Specification of pandas data creation.
- Parameters:
name (str | None)
origin (type | None)
factory (Callable[[...], DataFrame | Series[Any]] | None)
fields (Fields)
- name: str | None = None#
Name of the specification.
- origin: type | None = None#
Original dataclass of the specification.
- factory: Callable[[...], DataFrame | Series[Any]] | None = None#
Factory for pandas data creation.
- fields: Fields = ()#
List of field specifications.
- class Tag(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]#
Bases:
Flag
Collection of tags for annotating types.
- ATTR = 1#
Tag for a type specifying an attribute field.
- DATA = 2#
Tag for a type specifying a data field.
- INDEX = 4#
Tag for a type specifying an index field.
- DTYPE = 8#
Tag for a type specifying a data type.
- MULTIPLE = 16#
Tag for a type specifying a multiple-item field.
- FIELD = 7#
Union of field-related tags.
- ANY = 31#
Union of all tags.
- annotates(tp: Any) bool [source]#
Check if the tag annotates a type hint.
- Parameters:
tp (Any)
- Return type:
bool
- asdataframe(obj: Any, *, factory: Any = None) Any #
Alias of
core.aspandas.asframe
.- Parameters:
obj (Any)
factory (Any)
- Return type:
Any
- asframe(obj: Any, *, factory: Any = None) Any [source]#
Create a DataFrame object from a dataclass object.
The return type will be determined by a factory defined as the
__pandas_factory__
attribute in the original dataclass ofobj
or thefactory
argument. If neither is specified, it defaults topandas.DataFrame
.- Parameters:
obj (Any) – Dataclass object that should have attribute, column, data, and/or index fields. If the original dataclass has the
__pandas_factory__
attribute, it will be used as a factory for the DataFrame creation.factory (Any)
- Keyword Arguments:
factory – Class or function for the DataFrame creation. It must take the same parameters as
pandas.DataFrame
, and return an object of it or its subclass. If passed, it will be preferentially used even if the original dataclass ofobj
has the__pandas_factory__
attribute.- Returns:
DataFrame object that complies with the original dataclass.
- Return type:
Any
- aspandas(obj: Any, *, factory: Any = None) Any [source]#
Create a DataFrame or Series object from a dataclass object.
Which data structure is created will be determined by a factory defined as the
__pandas_factory__
attribute in the original dataclass ofobj
or thefactory
argument. If a factory is a function, it must have an annotation of the return type.- Parameters:
obj (Any) – Dataclass object that should have attribute, column, data, and/or index fields. If the original dataclass has the
__pandas_factory__
attribute, it will be used as a factory for the data creation.factory (Any)
- Keyword Arguments:
factory – Class or function for the DataFrame or Series creation. It must take the same parameters as
pandas.DataFrame
orpandas.Series
, and return an object of it or its subclass. If it is a function, it must have an annotation of the return type. If passed, it will be preferentially used even if the original dataclass ofobj
has the__pandas_factory__
attribute.- Returns:
DataFrame or Series object that complies with the original dataclass.
- Raises:
ValueError – Raised if no factory is found or the return type cannot be inferred from a factory when it is a function.
- Return type:
Any
- asseries(obj: Any, *, factory: Any = None) Any [source]#
Create a Series object from a dataclass object.
The return type will be determined by a factory defined as the
__pandas_factory__
attribute in the original dataclass ofobj
or thefactory
argument. If neither is specified, it defaults topandas.Series
.- Parameters:
obj (Any) – Dataclass object that should have attribute, column, data, and/or index fields. If the original dataclass has the
__pandas_factory__
attribute, it will be used as a factory for the Series creation.factory (Any)
- Keyword Arguments:
factory – Class or function for the Series creation. It must take the same parameters as
pandas.Series
, and return an object of it or its subclass. If passed, it will be preferentially used even if the original dataclass ofobj
has the__pandas_factory__
attribute.- Returns:
Series object that complies with the original dataclass.
- Return type:
Any