Skip to content

Add is_type_compatible for Index & MultiIndex #1765

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 3 commits into from
Sep 20, 2020
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
18 changes: 18 additions & 0 deletions databricks/koalas/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,24 @@ def is_object(self):
"""
return is_object_dtype(self.dtype)

def is_type_compatible(self, kind):
"""
Whether the index type is compatible with the provided type.

Examples
--------
>>> kidx = ks.Index([1, 2, 3])
>>> kidx.is_type_compatible('integer')
True

>>> kidx = ks.Index([1.0, 2.0, 3.0])
>>> kidx.is_type_compatible('integer')
False
>>> kidx.is_type_compatible('floating')
True
"""
return kind == self.inferred_type

def dropna(self):
"""
Return Index or MultiIndex without NA/NaN values
Expand Down
2 changes: 0 additions & 2 deletions databricks/koalas/missing/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class MissingPandasLikeIndex(object):
intersection = _unsupported_function("intersection")
is_ = _unsupported_function("is_")
is_lexsorted_for_tuple = _unsupported_function("is_lexsorted_for_tuple")
is_type_compatible = _unsupported_function("is_type_compatible")
join = _unsupported_function("join")
map = _unsupported_function("map")
putmask = _unsupported_function("putmask")
Expand Down Expand Up @@ -124,7 +123,6 @@ class MissingPandasLikeMultiIndex(object):
is_ = _unsupported_function("is_")
is_lexsorted = _unsupported_function("is_lexsorted")
is_lexsorted_for_tuple = _unsupported_function("is_lexsorted_for_tuple")
is_type_compatible = _unsupported_function("is_type_compatible")
join = _unsupported_function("join")
map = _unsupported_function("map")
putmask = _unsupported_function("putmask")
Expand Down
32 changes: 32 additions & 0 deletions databricks/koalas/tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,38 @@ def test_inferred_type(self):
kmidx = ks.from_pandas(pmidx)
self.assert_eq(pmidx.inferred_type, kmidx.inferred_type)

def test_is_type_compatible(self):
data_types = ["integer", "floating", "string", "boolean"]
# Integer
pidx = pd.Index([1, 2, 3])
kidx = ks.from_pandas(pidx)
for data_type in data_types:
self.assert_eq(pidx.is_type_compatible(data_type), kidx.is_type_compatible(data_type))

# Floating
pidx = pd.Index([1.0, 2.0, 3.0])
kidx = ks.from_pandas(pidx)
for data_type in data_types:
self.assert_eq(pidx.is_type_compatible(data_type), kidx.is_type_compatible(data_type))

# String
pidx = pd.Index(["a", "b", "c"])
kidx = ks.from_pandas(pidx)
for data_type in data_types:
self.assert_eq(pidx.is_type_compatible(data_type), kidx.is_type_compatible(data_type))

# Boolean
pidx = pd.Index([True, False, True, False])
kidx = ks.from_pandas(pidx)
for data_type in data_types:
self.assert_eq(pidx.is_type_compatible(data_type), kidx.is_type_compatible(data_type))

# MultiIndex
pmidx = pd.MultiIndex.from_tuples([("a", "x")])
kmidx = ks.from_pandas(pmidx)
for data_type in data_types:
self.assert_eq(pmidx.is_type_compatible(data_type), kmidx.is_type_compatible(data_type))

def test_asi8(self):
# Integer
pidx = pd.Index([1, 2, 3])
Expand Down