API¶
dataclassframe
¶
-
class
dataclassframe.
DataClassFrame
(*args, **kwds)[source]¶ -
__init__
(record_class: Type[RecordT], data: Iterable[RecordT], index: Union[None, str, List[str]] = None)[source]¶ Container of dataclasses.
- Parameters
record_class – The dataclasses class of each record
data – An iterable of dataclass records
index – Fields of the dataclass to use as indexes
-
copy
(deep: bool = True) → dataclassframe.dataclassframe_.DataClassFrame[RecordT][source]¶ Copy self
- Parameters
deep – Perform deep copy or not
Returns: copy of DataClassFrame
-
classmethod
from_dataframe
(record_class: Type[RecordT], dataframe: Optional[pandas.core.frame.DataFrame] = None, index: Union[None, str, List[str]] = None)[source]¶ Create a DataClassFrame using a Pandas DataFrame
- Parameters
record_class – The dataclasses class of each record
dataframe – A Pandas DataFrame of dataframe
index – Fields of the dataclass to use as indexes
Returns: DataClassFrame
-
head
(n: int = 5) → dataclassframe.dataclassframe_.DataClassFrame[RecordT][source]¶ Provide first n rows of self
- Parameters
n – First n rows to output
Returns: DataClassFrame of head
-
to_dataframe
() → pandas.core.frame.DataFrame[source]¶ Convert to dataframe. Copy dataframe to prevent side-effects.
Returns: Pandas DataFrame of same dataframe
-
property
at
¶ Access or set a single element using a dictionary like key(s). The key or key combination must index a unique record otherwise a KeyError is raised.
Returns: A record of type RecordT
Examples
Access record ‘a’ using the first field index:
>>> self.at['a']
Access record ‘b’ using the second field index:
>>> self.at[:, 'b']
Access record with joint key (‘c’, ‘d’):
>>> self.at['c', 'd']
Set record with joint key (‘a’, ‘f’):
>>> self.at['a', 'f'] = RecordT(foo='a', bar='f')
-
property
cols
¶ Access or set a column as a Pandas Series
- Returns
Pandas Series of column
Examples
Access column a:
>>> self.cols.a
Sum column a:
>>> self.cols.a.sum()
Set all values in column a to 0:
>>> self.cols.a = 0
-
property
iat
¶ Access or set a single element using positional index.
Returns: A record of type RecordT
Examples
Access second element:
>>> self.iat[1]
Access last element:
>>> self.iat[-1]
Set record at position 0:
>>> self.iat[0] = RecordT(foo='a', bar=1)
-