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 all 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
35 changes: 35 additions & 0 deletions databricks/koalas/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
from databricks.koalas.series import Series, first_series
from databricks.koalas.spark.accessors import SparkIndexMethods
from databricks.koalas.utils import (
combine_frames,
compare_disallow_null,
default_session,
is_name_like_tuple,
Expand Down Expand Up @@ -3318,6 +3319,40 @@ def item(self) -> Tuple[Scalar, ...]:
"""
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
--------
>>> from databricks.koalas.config import set_option, reset_option
>>> set_option("compute.ops_on_diff_frames", True)

>>> 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

>>> kmidx2 = ks.MultiIndex.from_tuples([("a", "x"), ("b", "y"), ("c", "j")])
>>> kmidx1.equal_levels(kmidx2)
False

>>> reset_option("compute.ops_on_diff_frames")
"""
nlevels = self.nlevels
if nlevels != other.nlevels:
return False
self_frame = self.sort_values().to_frame()
other_frame = other.sort_values().to_frame()
with option_context("compute.ops_on_diff_frames", True):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might avoid force enabling compute.ops_on_diff_frames. let's see.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, thanks for the review! Let me resolve the comments

combined = combine_frames(self_frame, other_frame)

sdf = combined._internal.spark_frame
that_index_name = combined["that"]._internal.data_spark_column_names[0]
that_index_scol = scol_for(sdf, that_index_name)

return len(sdf.filter(that_index_scol.isNull()).head(1)) == 0

def intersection(self, other) -> "MultiIndex":
"""
Form the intersection of two Index objects.
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 @@ -103,7 +103,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
23 changes: 23 additions & 0 deletions databricks/koalas/tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1889,6 +1889,29 @@ def test_multiindex_is_unique(self):

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))

pmidx2 = pd.MultiIndex.from_tuples([("a", "x"), ("b", "y"), ("c", "j")])
kmidx2 = ks.from_pandas(pmidx2)
self.assert_eq(pmidx1.equal_levels(pmidx2), kmidx1.equal_levels(kmidx2))

pmidx2 = pd.MultiIndex.from_tuples([("a", "x"), ("b", "y"), ("a", "x")])
kmidx2 = ks.from_pandas(pmidx2)
self.assert_eq(pmidx1.equal_levels(pmidx2), kmidx1.equal_levels(kmidx2))

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))

pmidx2 = pd.MultiIndex.from_tuples([("a", "x", "q"), ("b", "y", "w"), ("c", "z", "e")])
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

def test_view(self):
pidx = pd.Index([1, 2, 3, 4], name="Koalas")
kidx = ks.from_pandas(pidx)
Expand Down
1 change: 1 addition & 0 deletions docs/source/reference/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ MultiIndex Modifying and computations
:toctree: api/

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