pandas.DataFrame.dex.to_indexset#
- DataFrame.dex.to_indexset()#
Cast a DataFrame into an IndexSet1D/IndexSetND.
Note: The docplex-extensions pacakge has to be imported first to use this method with pandas.
- Returns:
- IndexSet1D or IndexSetND
A single-column DataFrame will be cast into an IndexSet1D consisting of its column values. The column name will be set as the IndexSet1D.name attribute (as str).
A multi-column DataFrame will be cast into an IndexSetND consisting of tuples, each containing row values. The column names will be set as the IndexSetND.names attribute (as list[str]).
- Raises:
- ValueError
If the DataFrame is empty.
- TypeError
If the DataFrame has non-scalar value(s) (any iterable except string).
- ValueError
If the DataFrame has duplicate rows.
See also
pandas.Series.dex.to_indexsetCast a Series into an IndexSet1D.
pandas.Index.dex.to_indexsetCast an Index into an IndexSet1D/IndexSetND.
Examples
>>> import pandas as pd >>> import docplex_extensions # required to access this method
Casting a single-column DataFrame into an IndexSet1D
>>> warehouses = pd.DataFrame(['WH-A', 'WH-B', 'WH-C'], columns=['WAREHOUSE']) >>> warehouses WAREHOUSE 0 WH-A 1 WH-B 2 WH-C
>>> warehouses.dex.to_indexset() IndexSet1D: (WAREHOUSE) ['WH-A', 'WH-B', 'WH-C']
Casting a multi-column DataFrame into an IndexSetND
>>> df = pd.DataFrame( ... { ... 'PRODUCT': ['chair', 'chair', 'desk', 'desk'], ... 'WAREHOUSE': ['WH-A', 'WH-B', 'WH-B', 'WH-C'], ... 'PERIOD': [0, 1, 0, 1], ... } ... ) >>> df PRODUCT WAREHOUSE PERIOD 0 chair WH-A 0 1 chair WH-B 1 2 desk WH-B 0 3 desk WH-C 1
>>> df.dex.to_indexset() IndexSetND: (PRODUCT, WAREHOUSE, PERIOD) [('chair', 'WH-A', 0), ('chair', 'WH-B', 1), ('desk', 'WH-B', 0), ('desk', 'WH-C', 1)]
>>> df.set_index('WAREHOUSE')[['PRODUCT', 'PERIOD']].dex.to_indexset() IndexSetND: (PRODUCT, PERIOD) [('chair', 0), ('chair', 1), ('desk', 0), ('desk', 1)]