Open larrymouse opened 4 years ago
I guess I have the same problem.
Creating two DFs, one with multiindex, and one without:
import pandas as pd
import numpy as np
from datetime import datetime
index = [datetime(2000, 1, 1, 0, 0), datetime(2000, 1, 1, 0, 1), datetime(2000, 1, 1, 0, 2)]
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = list(zip(*arrays))
columns_mi = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
columns = ["A", "B", "C", "D", "E", "F", "G", "H"]
df = pd.DataFrame(data=np.random.randn(3, 8), index=index, columns=columns)
df_mi = pd.DataFrame(data=np.random.randn(3, 8), index=index, columns=columns_mi)
Converting the 'normal' DataFrame to json and back works fine (both in split and table orientation):
s = df.to_json(orient="split")
pd.read_json(s, orient="split")
s = df.to_json(orient="table")
pd.read_json(s, orient="table")
But doing the same with the multiindex one doesn't.
s = df_mi.to_json(orient="split")
pd.read_json(s, orient="split")
Raises: TypeError: <class 'tuple'> is not convertible to datetime
s = df_mi.to_json(orient="table")
pd.read_json(s, orient="table")
Raises: NotImplementedError: orient='table' is not supported for MultiIndex
.
I'm trying to send multiindexed dataframes over HTTP; any suggestions on how I can do that?
I submitted a PR for the author issue.
About janheindejong issues,
Raises: TypeError: <class 'tuple'> is not convertible to datetime
This may be a dupe of #4889, MultiIndex Dataframes and Series do not seem to be handled by read_json(orient='split')
.
NotImplementedError: orient='table' is not supported for MultiIndex
orient='table'
does not support MultiIndex columns on Dataframes.
Same question, I'm trying to send multiindexed dataframes over HTTP.
When i serialise multi-index dataframe to an json by split or table orient, tuple inside the index would be converted to list. But when i try to deserialise it, it raise TypeError: unhashable type: 'list' ;
Any suggestions on how I can do that?
it's year 2024 and there is still no generic support to dump dataframe to json and get it back ?
NotImplementedError: orient='table' is not supported for MultiIndex columns
Code Sample, a copy-pastable example if possible
Problem description
I have a DataFrame that has a MultiIndex, even though that index only has one level and could easily be a normal index, I am receiving this table from ipyaggrid, so am not in control of that.
Saving this table to json with df.to_json(orient='table) and then loading from pandas.read_json(js, orient='table) leads to either an exception or the index being changed to NaN values.
Neither of these outcomes seems desirable or correct.
I have a workaround to intercept DataFrames with this feature and convert their index so a flat Index, but it would be good if the table serialisation just worked.
I was drawn to orient='Table' by the comments on this similar issue, my main requirement here is to save/load the DataFrame preserving its row and column order. You don't get this with the standard to_json/from_json modes https://github.com/pandas-dev/pandas/issues/4889
Expected Output
new_df for scenario1 and scenario2 to should have a MultiIndex, in the first scenario with a level that has no name and in the second scenario with level called 'ind1'
Output of
pd.show_versions()