Skip to content

Incorrect identically-labeled DataFrame objects Exception comes with df.compare method  #50083

Closed
@abhid001

Description

@abhid001

When we have exact same columns in source and target dataframes and distinct number of rows then we are seeing below exception which according to me is misleading.

"ValueError: Can only compare identically-labeled DataFrame objects"

#CODE :

#define DataFrames
df1 = pd.DataFrame({'points': [25, 12, 15, 14],
                   'assists': [5, 7, 13, 12]})

df2 = pd.DataFrame({'points': [25, 12, 15],
                    'assists': [5, 7, 13]})

print(df1)

   points  assists
0      25        5
1      12        7
2      15       13
3      14       12

print(df2)

   points  assists
0      25        5
1      12        7
2      15       13

# Compare both dataframes

df1.compare(df2)

+++++++++++++++++++++++++++++++++ERROR+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/frame.py", line 7117, in compare
    keep_equal=keep_equal,
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/generic.py", line 8424, in compare
    mask = ~((self == other) | (self.isna() & other.isna()))
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/ops/common.py", line 69, in new_method
    return method(self, other)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/arraylike.py", line 32, in __eq__
    return self._cmp_method(other, operator.eq)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/frame.py", line 6851, in _cmp_method
    self, other = ops.align_method_FRAME(self, other, axis, flex=False, level=None)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/ops/__init__.py", line 289, in align_method_FRAME
    "Can only compare identically-labeled DataFrame objects"
ValueError: Can only compare identically-labeled DataFrame objects
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions