Home > Net >  How to test dataframes are equal when one is from JSON?
How to test dataframes are equal when one is from JSON?

Time:11-23

I have 2 dataframes. One loaded from parquet, the other generated from a dictionary in JSON.

I keep getting this error even though the indexes are the same i.e. 0-5.

E           AssertionError: DataFrame.index are different
E           
E           DataFrame.index classes are not equivalent
E           [left]:  RangeIndex(start=0, stop=5, step=1)
E           [right]: Index(['0', '1', '2', '3', '4'], dtype='object')

does anyone know what i'd have to do to the right or left dataframe to get this to work?

the line is

pd.testing.assert_frame_equal(de_df.head(), pd.DataFrame(expected_de_json))

CodePudding user response:

Reset their indexes.

df = df.reset_index(drop=True)

Under some conditions, pandas will reset the index, but store the old index in a new column. drop=True will ensure that this does not happen.

  • Related