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 methodsconvert()andvanilla(). 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
dfwill have its__class__changed toclsbut 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 callingpd.DataFrame(df). Ifdfis a list (Iterable) of DataFrames, will callpd.concaton them; for this,ignore_index=Trueis passed. If the list is empty, will returnnew_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 ofCustomers.convert(...)if a DataFrame instance is provided. You may add and process keyword arguments, but keyword args forpd.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 concatenatingkwargs β 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__.converton 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 callconverton most calls.Examples
df = MyDf(data).apply(my_fn, axis=1).retype() # make sure it's still validdf = MyDf(data).groupby(...).retype() # we maybe changed the index; fix it
- Returns
A copy