typeddfs.base_dfs

Defines the superclasses of the types TypedDf and UntypedDf.

Module Contents

class typeddfs.base_dfs.BaseDf(data=None, index=None, columns=None, dtype=None, copy=False)

An abstract DataFrame type that has a way to convert and de-convert. A subclass of typeddfs.abs_dfs.AbsDf, it has methods convert() and vanilla(). but no implementation or enforcement of typing.

__getitem__(self, item)

Finds an index level or or column, returning the Series, DataFrame, or value. Note that typeddfs forbids duplicate column names, as well as column names and index levels sharing names.

classmethod convert(cls, df: pandas.DataFrame) __qualname__

Converts a vanilla Pandas DataFrame to cls.

Note

The argument df will have its __class__ changed to cls but will otherwise be unaffected.

Returns

A copy

classmethod of(cls, df, *args, keys: Optional[Iterable[str]] = None, **kwargs) __qualname__

Construct or convert a DataFrame, returning this type. Delegates to convert() for DataFrames, or tries first constructing a DataFrame by calling pd.DataFrame(df). If df is a list (Iterable) of DataFrames, will call pd.concat on them; for this, ignore_index=True is passed. If the list is empty, will return new_df().

May be overridden to accept more types, such as a string for database lookup. For example, Customers.of("john") could return a DataFrame for a database customer, or return the result of Customers.convert(...) if a DataFrame instance is provided. You may add and process keyword arguments, but keyword args for pd.DataFrame.__init__ should be passed along to that constructor.

Parameters
  • df – A DataFrame, list of DataFrames, or something to be passed to pd.DataFrame.

  • keys – Labels for the DataFrames (if passed a sequence of them) to use as attr keys; if None, attrs will be empty ({}) if concatenating

  • kwargs – Passed to pd.DataFrame.__init__; can be handled directly by this method for specialized construction, database lookup, etc.

Returns

A new DataFrame; see convert() for more info.

retype(self) __qualname__

Calls self.__class__.convert on this DataFrame. This is useful to call at the end of a chain of DataFrame functions, where the type is preserved but the DataFrame may no longer be valid under this type’s rules. This can occur because, for performance, typeddfs does not call convert on most calls.

Examples

  • df = MyDf(data).apply(my_fn, axis=1).retype()  # make sure it's still valid

  • df = MyDf(data).groupby(...).retype()  # we maybe changed the index; fix it

Returns

A copy