# Import statement
from hottbox.pdtools import tensor_to_pd
tensor_to_pd
(tensor, col_name=None)[source]¶Represent tensor as a multi-index pandas dataframe
Tensor to be represented as a multi-index dataframe
Column label to use for resulting dataframe
Multi-index data frame
If tensor
is not in normal state: tensor.in_normal_state is False
.
Examples
Conversion of a tensor with default meta information
>>> import numpy as np
>>> from hottbox.core import Tensor
>>> from hottbox.pdtools import tensor_to_pd
>>> data = np.arange(8).reshape(2, 2, 2)
>>> tensor = Tensor(data)
>>> print(tensor.data)
[[[0 1]
[2 3]]
[[4 5]
[6 7]]]
>>> tensor.modes
[Mode(name='mode-0', index=None),
Mode(name='mode-1', index=None),
Mode(name='mode-2', index=None)]
>>> df = tensor_to_pd(tensor)
>>> print(df)
Values
mode-0 mode-1 mode-2
0 0 0 0
1 1
1 0 2
1 3
1 0 0 4
1 5
1 0 6
1 7
Conversion of a tensor with specified mode names
>>> import numpy as np
>>> from hottbox.core import Tensor
>>> from hottbox.pdtools import tensor_to_pd
>>> data = np.arange(8).reshape(2, 2, 2)
>>> tensor = Tensor(data, mode_names=["Year", "Month", "Day"])
>>> print(tensor.data)
[[[0 1]
[2 3]]
[[4 5]
[6 7]]]
>>> tensor.modes
[Mode(name='Year', index=None),
Mode(name='Month', index=None),
Mode(name='Day', index=None)]
>>> df = tensor_to_pd(tensor)
>>> print(df)
Values
Year Month Day
0 0 0 0
1 1
1 0 2
1 3
1 0 0 4
1 5
1 0 6
1 7
Conversion of a tensor with specified mode names and mode index
>>> import numpy as np
>>> from hottbox.core import Tensor
>>> from hottbox.pdtools import tensor_to_pd
>>> data = np.arange(8).reshape(2, 2, 2)
>>> mode_index = {0: [2005, 2010],
... 1: ["Jan", "Feb"],
... 2: ["Mon", "Wed"],
... }
>>> tensor = Tensor(data, mode_names=["Year", "Month", "Day"])
>>> tensor.set_mode_index(mode_index)
>>> print(tensor.data)
[[[0 1]
[2 3]]
[[4 5]
[6 7]]]
>>> tensor.modes
[Mode(name='Year', index=[2005, 2010]),
Mode(name='Month', index=['Jan', 'Feb']),
Mode(name='Day', index=['Mon', 'Wed'])]
>>> df = tensor_to_pd(tensor)
>>> print(df)
Values
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
>>> df = tensor_to_pd(tensor, col_name="Population")
>>> 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