Skip to content

[ArrayManager] Implement .equals method #39721

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

Merged
merged 1 commit into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions pandas/core/internals/array_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
)
from pandas.core.dtypes.dtypes import ExtensionDtype, PandasDtype
from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries
from pandas.core.dtypes.missing import isna
from pandas.core.dtypes.missing import array_equals, isna

import pandas.core.algorithms as algos
from pandas.core.arrays import ExtensionArray
Expand Down Expand Up @@ -829,9 +829,16 @@ def _make_na_array(self, fill_value=None):
values.fill(fill_value)
return values

def equals(self, other: object) -> bool:
# TODO
raise NotImplementedError
def _equal_values(self, other) -> bool:
"""
Used in .equals defined in base class. Only check the column values
assuming shape and indexes have already been checked.
"""
for left, right in zip(self.arrays, other.arrays):
if not array_equals(left, right):
return False
else:
return True

def unstack(self, unstacker, fill_value) -> ArrayManager:
"""
Expand Down
22 changes: 22 additions & 0 deletions pandas/core/internals/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,25 @@ def reindex_axis(
consolidate=consolidate,
only_slice=only_slice,
)

def _equal_values(self: T, other: T) -> bool:
"""
To be implemented by the subclasses. Only check the column values
assuming shape and indexes have already been checked.
"""
raise AbstractMethodError(self)

def equals(self, other: object) -> bool:
"""
Implementation for DataFrame.equals
"""
if not isinstance(other, DataManager):
return False

self_axes, other_axes = self.axes, other.axes
if len(self_axes) != len(other_axes):
return False
if not all(ax1.equals(ax2) for ax1, ax2 in zip(self_axes, other_axes)):
return False

return self._equal_values(other)
15 changes: 5 additions & 10 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1395,16 +1395,11 @@ def take(self, indexer, axis: int = 1, verify: bool = True, convert: bool = True
consolidate=False,
)

def equals(self, other: object) -> bool:
if not isinstance(other, BlockManager):
return False

self_axes, other_axes = self.axes, other.axes
if len(self_axes) != len(other_axes):
return False
if not all(ax1.equals(ax2) for ax1, ax2 in zip(self_axes, other_axes)):
return False

def _equal_values(self: T, other: T) -> bool:
"""
Used in .equals defined in base class. Only check the column values
assuming shape and indexes have already been checked.
"""
if self.ndim == 1:
# For SingleBlockManager (i.e.Series)
if other.ndim != 1:
Expand Down
14 changes: 5 additions & 9 deletions pandas/tests/frame/methods/test_equals.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import numpy as np

import pandas.util._test_decorators as td

from pandas import DataFrame, date_range
import pandas._testing as tm

# TODO(ArrayManager) implement equals
pytestmark = td.skip_array_manager_not_yet_implemented


class TestEquals:
def test_dataframe_not_equal(self):
Expand All @@ -16,13 +11,14 @@ def test_dataframe_not_equal(self):
df2 = DataFrame({"a": ["s", "d"], "b": [1, 2]})
assert df1.equals(df2) is False

def test_equals_different_blocks(self):
def test_equals_different_blocks(self, using_array_manager):
# GH#9330
df0 = DataFrame({"A": ["x", "y"], "B": [1, 2], "C": ["w", "z"]})
df1 = df0.reset_index()[["A", "B", "C"]]
# this assert verifies that the above operations have
# induced a block rearrangement
assert df0._mgr.blocks[0].dtype != df1._mgr.blocks[0].dtype
if not using_array_manager:
# this assert verifies that the above operations have
# induced a block rearrangement
assert df0._mgr.blocks[0].dtype != df1._mgr.blocks[0].dtype

# do the real tests
tm.assert_frame_equal(df0, df1)
Expand Down