MulDataFrame#

class muldataframe.MulDataFrame(data, index: DataFrame = None, columns: DataFrame = None, index_init: Literal['overlap'] | Literal['override'] | Literal['align'] = None, columns_init: Literal['overlap'] | Literal['override'] | Literal['align'] = None, both_init: Literal['overlap'] | Literal['override'] | Literal['align'] = None, index_copy=True, columns_copy=True, both_copy=None, data_copy=None)#

A multi-index dataframe with the index and the columns being pandas dataframes.

It also has an underlying values dataframe that is not directly accessible. Its values are the same as the values of the values dataframe.

Parameters#

datapandas.DataFrame, ndarray (structured or homogeneous), Iterable, dict

either a pandas DataFrame or the same kind of data argument as required in the pandas.DataFrame constructor. The values dataframe is constructed from the data argument.

indexpandas.DataFrame

If index is None, construct an empty index dataframe using the index of the values dataframe as its index.

columnspandas.DataFrame

If columns is None, construct an empty columns dataframe using the columns of the values dataframe as its index.

index_initLiteral[‘overlap’] | Literal[‘override’] | Literal[‘align’]

The option determins how to align the index of the index dataframe to the index of the values dataframe.

  • ‘overlap’ : the overlap of the index of the index dataframe and the index of the values dataframe is used to index the values and the index dataframes. This mode requires both indexes being unique.

  • ‘override’ : the index of the index dataframe overrides the index of the values dataframe. This mode requires both indexes’ lengths to be the same.

  • ‘align’ : the index of the index dataframe is used to index the values dataframe constructed from the data argument. The resulting indexed values dataframe is used as the final values dataframe. It requires the index of the values dataframe being uinque and include all the values in the index dataframe’s index.

  • None : the default behavior depends on the type of the data argument. If data implies a row index such as if it is a pandas.DataFrame or a pandas.Series object, the mode will be 'overlap`. Otherwise, it will be 'override'.

Of note, when data implies a row index and this index is the same as the index of the index dataframe, the 'override' mode will be used even if other modes are specified. In all three modes, the name of the initialized primary index will be the same as the name of the index of the index dataframe.

columns_initLiteral[‘overlap’] | Literal[‘override’] | Literal[‘align’]

The option determins how to align the index of the columns dataframe to the columns of the values dataframe.

  • ‘overlap’ : the overlap of the index of the index dataframe and the index of the values dataframe is used to index the values and the index dataframes. This mode requires both indexes being unique.

  • ‘override’ : the index of the columns dataframe overrides the columns of the values dataframe. This mode requires both indexes’ lengths to be the same.

  • ‘align’ : the index of the columns dataframe is used to index the columns of the values dataframe constructed from the data argument. The resulting indexed values dataframe is used as the final values dataframe. It requires the columns of the values dataframe being unique and the labels of the columns dataframe’s index exist in the columns of the values dataframe.

  • None : the default behavior depends on the type of the data argument. If data implies a column index such as if it is a pandas.DataFrame, a dict of list-like objects or a list of dict objects, the mode will be 'overlap`. Otherwise, it will be 'override'.

Of note, when data implies a columns index and this index is the same as the index of the columns dataframe, the 'override' mode will be used even if other modes are specified. In all three modes, the name of the initialized primary columns will be the same as the name of the index of the columns dataframe.

both_initLiteral[‘overlap’] | Literal[‘override’] | Literal[‘align’]

It overrides index_init and columns_init with the same value.

index_copybool

whether to create a copy of the index argument.

columns_copybool

whether to create a copy of the columns argument.

both_copybool

It overrides index_copy and columns_copy with the same value.

data_copybool, default None

Wether to copy data. It behaves the same as the copy argument in pandas.DataFrame.__init__

Examples#

Construct a muldataframe. Notice that the index of the index muldataframe and the index of the values muldataframe are the same and the index of the columns dataframe and the columns of the values dataframe are the same.

>>> import pandas as pd
>>> import muldataframe as md
>>> index = pd.DataFrame([[1,2],[3,6],[5,6]],
                 index=['a','b','b'],
                 columns=['x','y'])
>>> columns = pd.DataFrame([[5,7],[3,6]],
                    index=['c','d'],
                    columns=['f','g'])
>>> mf = MulDataFrame([[1,2],[8,9],[8,10]],index=index,columns=columns)
>>> mf
(3, 2)    g  7   6
          f  5   3
             c   d
--------  ---------
   x  y      c   d
a  1  2   a  1   2
b  3  6   b  8   9
b  5  6   b  8  10