datamatrix.convert
Convert between DataMatrix
objects and types of data structures (notably pandas.DataFrame
).
function from_json(s)
Requires json_tricks
Creates a DataMatrix from a json
string.
Arguments:
s
-- A json string.- Type: str
Returns:
A DataMatrix.
- Type: DataMatrix.
function from_pandas(df)
Converts a pandas DataFrame to a DataMatrix.
Example:
import pandas as pd
from datamatrix import convert
df = pd.DataFrame( {'col' : [1,2,3] } )
dm = convert.from_pandas(df)
print(dm)
Output:
+---+-----+
| # | col |
+---+-----+
| 0 | 1 |
| 1 | 2 |
| 2 | 3 |
+---+-----+
Arguments:
df
-- No description- Type: DataFrame
Returns:
No description
- Type: DataMatrix
function to_json(dm)
Requires json_tricks
Creates (serializes) a json
string from a DataMatrix.
Arguments:
dm
-- A DataMatrix to serialize.- Type: DataMatrix
Returns:
A json string.
- Type: str
function to_pandas(dm)
Converts a DataMatrix to a pandas DataFrame.
Example:
from datamatrix import DataMatrix, convert
dm = DataMatrix(length=3)
dm.col = 1, 2, 3
df = convert.to_pandas(dm)
print(df)
Output:
col
0 1
1 2
2 3
Arguments:
dm
-- No description- Type: DataMatrix
Returns:
No description
- Type: DataFrame
function wrap_pandas(fnc)
A decorator for pandas functions. It converts a DataMatrix to a DataFrame, passes it to a function, and then converts the returned DataFrame back to a DataMatrix.
Example:
import pandas as pd
from datamatrix import convert as cnv
pivot_table = cnv.wrap_pandas(pd.pivot_table)
Arguments:
fnc
-- A function that takes a DataFrame as first argument and returns a DataFrame as sole return argument.- Type: callable
Returns:
A function takes a DataMatrix as first argument and returns a DataMatrix as sole return argument.