pandas_dataclasses package#

Subpackages#

Module contents#

class As[source]#

Bases: Generic[TPandas]

Pandas data creation by a classmethod (new).

new#

Class property decorator dedicated to As.new.

AsDataFrame#

Alias of core.mixins.AsFrame.

alias of As[DataFrame]

class Spec(name: Optional[str] = None, origin: Optional[type] = None, factory: Optional[Callable[[...], Union[DataFrame, Series[Any]]]] = None, fields: Fields = ())[source]#

Bases: object

Specification of pandas data creation.

Parameters:
  • name (Optional[str]) –

  • origin (Optional[type]) –

  • factory (Optional[Callable[[...], Union[DataFrame, Series[Any]]]]) –

  • fields (Fields) –

factory: Optional[Callable[[...], Union[DataFrame, Series[Any]]]] = None#

Factory for pandas data creation.

fields: Fields = ()#

List of field specifications.

classmethod from_dataclass(dataclass: type) Self[source]#

Create a specification from a data class.

Parameters:

dataclass (type) –

Return type:

Self

name: Optional[str] = None#

Name of the specification.

origin: Optional[type] = None#

Original dataclass of the specification.

update(obj: Any) Self[source]#

Update the specification by an object.

Parameters:

obj (Any) –

Return type:

Self

class Tag(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]#

Bases: Flag

Collection of tags for annotating types.

ANY = 31#

Union of all tags.

ATTR = 1#

Tag for a type specifying an attribute field.

DATA = 2#

Tag for a type specifying a data field.

DTYPE = 8#

Tag for a type specifying a data type.

FIELD = 7#

Union of field-related tags.

INDEX = 4#

Tag for a type specifying an index field.

MULTIPLE = 16#

Tag for a type specifying a multiple-item field.

annotates(tp: Any) bool[source]#

Check if the tag annotates a type hint.

Parameters:

tp (Any) –

Return type:

bool

classmethod creates(obj: Any) TypeGuard[Self][source]#

Check if Tag is the type of an object.

Parameters:

obj (Any) –

Return type:

TypeGuard[Self]

classmethod union(tags: Iterable[Self]) Self[source]#

Create a tag as an union of tags.

Parameters:

tags (Iterable[Self]) –

Return type:

Self

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 of obj or the factory argument. If neither is specified, it defaults to pandas.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 of obj 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 of obj or the factory 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 or pandas.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 of obj 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 of obj or the factory argument. If neither is specified, it defaults to pandas.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 of obj has the __pandas_factory__ attribute.

Returns:

Series object that complies with the original dataclass.

Return type:

Any