Skip to content
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

Implemented MultiIndex.equal_levels #1789

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions databricks/koalas/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2973,6 +2973,30 @@ def item(self):
"""
return self._kdf.head(2)._to_internal_pandas().index.item()

def equal_levels(self, other):
"""
Return True if the levels of both MultiIndex objects are the same

Examples
--------
>>> kmidx1 = ks.MultiIndex.from_tuples([("a", "x"), ("b", "y"), ("c", "z")])
>>> kmidx2 = ks.MultiIndex.from_tuples([("b", "y"), ("a", "x"), ("c", "z")])
>>> kmidx1.equal_levels(kmidx2)
True
"""
nlevels = self.nlevels
if nlevels != other.nlevels:
return False
self = self.sort_values()
other = other.sort_values()
itholic marked this conversation as resolved.
Show resolved Hide resolved
with ks.option_context("compute.ops_on_diff_frames", True):
for i in range(nlevels):
itholic marked this conversation as resolved.
Show resolved Hide resolved
self_level_values = self.get_level_values(i)
other_level_values = other.get_level_values(i)
if not self_level_values.equals(other_level_values):
return False
return True

@property
def inferred_type(self):
"""
Expand Down
1 change: 0 additions & 1 deletion databricks/koalas/missing/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ class MissingPandasLikeMultiIndex(object):
# Functions
argsort = _unsupported_function("argsort")
asof_locs = _unsupported_function("asof_locs")
equal_levels = _unsupported_function("equal_levels")
factorize = _unsupported_function("factorize")
format = _unsupported_function("format")
get_indexer = _unsupported_function("get_indexer")
Expand Down
13 changes: 13 additions & 0 deletions databricks/koalas/tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1581,3 +1581,16 @@ def test_multiindex_is_unique(self):
kdf = ks.from_pandas(pdf)

self.assertEqual(kdf.index.is_unique, expected)

def test_multiindex_equal_levels(self):
pmidx1 = pd.MultiIndex.from_tuples([("a", "x"), ("b", "y"), ("c", "z")])
pmidx2 = pd.MultiIndex.from_tuples([("b", "y"), ("a", "x"), ("c", "z")])
kmidx1 = ks.from_pandas(pmidx1)
kmidx2 = ks.from_pandas(pmidx2)

self.assert_eq(pmidx1.equal_levels(pmidx2), kmidx1.equal_levels(kmidx2))
itholic marked this conversation as resolved.
Show resolved Hide resolved

pmidx2 = pd.MultiIndex.from_tuples([("a", "x"), ("b", "y")])
kmidx2 = ks.from_pandas(pmidx2)

self.assert_eq(pmidx1.equal_levels(pmidx2), kmidx1.equal_levels(kmidx2))
1 change: 1 addition & 0 deletions docs/source/reference/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ MultiIndex Modifying and computations
:toctree: api/

MultiIndex.equals
MultiIndex.equal_levels
MultiIndex.identical
MultiIndex.drop
MultiIndex.copy
Expand Down