# Import statement
from hottbox.pdtools import pd_to_tensor
pd_to_tensor
(df, keep_index=True)[source]¶Represent multi-index pandas dataframe as a tensor
Multi-index dataframe with only one column of data
Keep level values of dataframe multi-index
Examples
>>> import numpy as np
>>> import pandas as pd
>>> from hottbox.pdtools import pd_to_tensor
>>> data = {'Year': [2005, 2005, 2005, 2005, 2010, 2010, 2010, 2010],
... 'Month': ['Jan', 'Jan', 'Feb', 'Feb', 'Jan', 'Jan', 'Feb', 'Feb'],
... 'Day': ['Mon', 'Wed', 'Mon', 'Wed', 'Mon', 'Wed', 'Mon', 'Wed'],
... 'Population': np.arange(8)
... }
>>> df = pd.DataFrame.from_dict(data)
>>> df.set_index(["Year", "Month", "Day"], inplace=True)
>>> print(df)
Population
Year Month Day
2005 Jan Mon 0
Wed 1
Feb Mon 2
Wed 3
2010 Jan Mon 4
Wed 5
Feb Mon 6
Wed 7
>>> tensor = pd_to_tensor(df)
>>> print(tensor.data)
[[[0 1]
[2 3]]
[[4 5]
[6 7]]]
>>> print(tensor)
This tensor is of order 3 and consists of 8 elements.
Sizes and names of its modes are (2, 2, 2) and ['Year', 'Month', 'Day'] respectively.
>>> tensor.modes
[Mode(name='Year', index=[2005, 2010]),
Mode(name='Month', index=['Jan', 'Feb']),
Mode(name='Day', index=['Mon', 'Wed'])]
>>> tensor = pd_to_tensor(df, keep_index=False)
>>> tensor.modes
[Mode(name='Year', index=None),
Mode(name='Month', index=None),
Mode(name='Day', index=None)]