typeddfs.typed_dfsο
Defines DataFrames with convenience methods and that enforce invariants.
Module Contentsο
- class typeddfs.typed_dfs.PlainTypedDfο
A trivial TypedDf that behaves like an untyped one.
- class typeddfs.typed_dfs.TypedDfο
A concrete BaseFrame that enforces conditions. Each subclass has required and reserved (optional) columns and index names. They may or may not permit additional columns or index names.
The constructor will require the conditions to pass but will not rearrange columns and indices. To do that, call
convert.Overrides a number of DataFrame methods that preserve the subclass. For example, calling
df.reset_index()will return aTypedDfof the same type asdf. If a condition would then fail, calluntyped()first.For example, suppose
MyTypedDfhas a required index name called βxyzβ. Then this will be fine as long asdfhas a column or index name calledxyz:MyTypedDf.convert(df). But callingMyTypedDf.convert(df).reset_index()will fail. You can put the column βxyzβ back into the index usingconvert:MyTypedDf.convert(df.reset_index()). Or, you can get a plain DataFrame (UntypedDf) back:MyTypedDf.convert(df).untyped().reset_index().To summarize: Call
untyped()before calling something that would result in anything invalid.- classmethod _check(cls, df) Noneο
- classmethod _check_has_required(cls, df: pandas.DataFrame) Noneο
- classmethod _check_has_unexpected(cls, df: pandas.DataFrame) Noneο
- classmethod convert(cls, df: pandas.DataFrame) __qualname__ο
Converts a vanilla Pandas DataFrame (or any subclass) to
cls. Explicitly sets the new copyβs __class__ to cls. Rearranges the columns and index names. For example, if a column indfis inself.reserved_index_names(), it will be moved to the index.- The new index names will be, in order:
required_index_names(), in orderreserved_index_names(), in orderany extras in
df, ifmore_indices_allowedis True
- Similarly, the new columns will be, in order:
required_columns(), in orderreserved_columns(), in orderany extras in
dfin the original, ifmore_columns_allowedis True
Note
Any column called
indexorlevel_0will be dropped automatically.- Parameters
df β The Pandas DataFrame or member of cls; will have its __class_ change but will otherwise not be affected
- Returns
A copy
- Raises
InvalidDfError β If a condition such as a required column or symmetry fails (specific subclasses)
TypeError β If
dfis not a DataFrame
- classmethod get_typing(cls) typeddfs.df_typing.DfTypingο
- meta(self) __qualname__ο
Drops the columns, returning only the index but as the same type.
- Returns
A copy
- Raises
InvalidDfError β If the result does not pass the typing of this class
- classmethod new_df(cls, reserved: Union[bool, Sequence[str]] = False) __qualname__ο
Returns a DataFrame that is empty but has the correct columns and indices.
- Parameters
reserved β Include reserved index/column names as well as required. If True, adds all reserved index levels and columns; You can also specify the exact list of columns and index names.
- Raises
InvalidDfError β If a function in
verificationsfails (returns False or a string).
- untyped(self) typeddfs.untyped_dfs.UntypedDfο
Makes a copy thatβs an UntypedDf. It wonβt have enforced requirements but will still have the convenience functions.
- Returns
A shallow copy with its __class__ set to an UntypedDf
- See:
vanilla()