larray.from_frame

larray.from_frame(df, sort_rows=False, sort_columns=False, parse_header=False, unfold_last_axis_name=False, fill_value=nan, meta=None, cartesian_prod=True, **kwargs)[source]

Converts Pandas DataFrame into LArray.

Parameters
dfpandas.DataFrame

Input dataframe. By default, name and labels of the last axis are defined by the name and labels of the columns Index of the dataframe unless argument unfold_last_axis_name is set to True.

sort_rowsbool, optional

Whether or not to sort the rows alphabetically (sorting is more efficient than not sorting). Must be False if cartesian_prod is set to True. Defaults to False.

sort_columnsbool, optional

Whether or not to sort the columns alphabetically (sorting is more efficient than not sorting). Must be False if cartesian_prod is set to True. Defaults to False.

parse_headerbool, optional

Whether or not to parse columns labels. Pandas treats column labels as strings. If True, column labels are converted into int, float or boolean when possible. Defaults to False.

unfold_last_axis_namebool, optional

Whether or not to extract the names of the last two axes by splitting the name of the last index column of the dataframe using \. Defaults to False.

fill_valuescalar, optional

Value used to fill cells corresponding to label combinations which are not present in the input DataFrame. Defaults to NaN.

metalist of pairs or dict or OrderedDict or Metadata, optional

Metadata (title, description, author, creation_date, …) associated with the array. Keys must be strings. Values must be of type string, int, float, date, time or datetime.

cartesian_prodbool, optional

Whether or not to expand the dataframe to a cartesian product dataframe as needed by LArray. This is an expensive operation but is absolutely required if you cannot guarantee your dataframe is already well formed. If True, arguments sort_rows and sort_columns must be set to False. Defaults to True.

Returns
LArray

See also

LArray.to_frame

Examples

>>> from larray import ndtest
>>> df = ndtest((2, 2, 2)).to_frame()
>>> df                                                                             # doctest: +NORMALIZE_WHITESPACE
c      c0  c1
a  b
a0 b0   0   1
   b1   2   3
a1 b0   4   5
   b1   6   7
>>> from_frame(df)
 a  b\c  c0  c1
a0   b0   0   1
a0   b1   2   3
a1   b0   4   5
a1   b1   6   7

Names of the last two axes written as before_last_axis_name\\last_axis_name

>>> df = ndtest((2, 2, 2)).to_frame(fold_last_axis_name=True)
>>> df                                                                             # doctest: +NORMALIZE_WHITESPACE
        c0  c1
a  b\c
a0 b0    0   1
   b1    2   3
a1 b0    4   5
   b1    6   7
>>> from_frame(df, unfold_last_axis_name=True)
 a  b\c  c0  c1
a0   b0   0   1
a0   b1   2   3
a1   b0   4   5
a1   b1   6   7