Skip to content

ENH: assert_frame_equal can produce unhelpful output #31190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
amilbourne opened this issue Jan 21, 2020 · 1 comment
Closed

ENH: assert_frame_equal can produce unhelpful output #31190

amilbourne opened this issue Jan 21, 2020 · 1 comment
Labels
Enhancement Error Reporting Incorrect or improved errors from pandas
Milestone

Comments

@amilbourne
Copy link
Contributor

Code Sample, a copy-pastable example if possible

import pandas as pd

df1 = pd.DataFrame({'a': [1, 2, 3], 'b': [10, 20, 30]}, index=[0, 1, 2])
df2 = pd.DataFrame({'a': [2, 1, 3], 'b': [20, 999, 30]}, index=[1, 0, 2])

print(df1)
print(df2)

pd.testing.assert_frame_equal(df1, df2, check_like=True)

Generates the output:

   a   b
0  1  10
1  2  20
2  3  30
   a    b
1  2   20
0  1  999
2  3   30
Traceback (most recent call last):
 <removed stack trace>
    raise AssertionError(msg)
AssertionError: DataFrame.iloc[:, 1] are different

DataFrame.iloc[:, 1] values are different (33.33333 %)
[left]:  [20, 10, 30]
[right]: [20, 999, 30]

Problem description

When running assert_frame_equal with check_like=True the error message can be unhelpful if the column or index order differs (or worse, both).

#29218 has greatly helped the issue with column reordering, although it could be insufficient if check_names is false and the column names differ (but this is probably a small edge case).

However, row (index) reordering is still an issue. As the output above demonstrates, it is not obvious that the left column has been reordered. In most cases the user could probably work it out but it is not as clear as it could be.

Possible Solution

One solution would be to explicitly state the index values in the output. This would probably have to affect the output of assert_series_equal as well. This would be quite a significant change to the output though so it may be a sledgehammer to crack a nut.

DataFrame.iloc[:, 1] values are different (33.33333 %)
[index]: [1, 0, 2]
[left]:  [20, 10, 30]
[right]: [20, 999, 30]

A side effect of including the index would be that you could avoid having to display the entire sequence for low numbers of differences in long sequences - although I'm not suggesting that be part of this change.

Is this worth doing and is the above a sensible solution?

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]
INSTALLED VERSIONS

commit : None
python : 3.7.5.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None

pandas : 0.25.2
numpy : 1.17.3
pytz : 2019.3
dateutil : 2.8.0
pip : 19.3.1
setuptools : 41.6.0.post20191030
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None

@jreback
Copy link
Contributor

jreback commented Mar 22, 2020

closed by #31435

@jreback jreback closed this as completed Mar 22, 2020
@jreback jreback added this to the 1.1 milestone Mar 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Error Reporting Incorrect or improved errors from pandas
Projects
None yet
Development

No branches or pull requests

2 participants