Skip to content

Commit e02427e

Browse files
HyukjinKwonueshin
authored andcommitted
Add is_boolean, is_categorical, is_floating, is_integer, is_interval, is_numeric and is_object in Index and MultiIndex (#795)
See https://pandas.pydata.org/pandas-docs/stable/reference/indexing.html
1 parent d0b4f5f commit e02427e

File tree

3 files changed

+94
-15
lines changed

3 files changed

+94
-15
lines changed

databricks/koalas/indexes.py

+80-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
from typing import Any, List, Optional, Tuple, Union
2323

2424
import pandas as pd
25-
from pandas.api.types import is_list_like
25+
from pandas.api.types import is_list_like, is_interval_dtype, is_bool_dtype, \
26+
is_categorical_dtype, is_integer_dtype, is_float_dtype, is_numeric_dtype, is_object_dtype
27+
2628
from pyspark import sql as spark
2729
from pyspark.sql import functions as F
2830

@@ -239,6 +241,83 @@ def to_series(self, name: Union[str, Tuple[str, ...]] = None) -> Series:
239241
column_index_names=None),
240242
anchor=kdf)
241243

244+
def is_boolean(self):
245+
"""
246+
Return if the current index type is a boolean type.
247+
248+
Examples
249+
--------
250+
>>> ks.DataFrame({'a': [1]}, index=[True]).index.is_boolean()
251+
True
252+
"""
253+
return is_bool_dtype(self.dtype)
254+
255+
def is_categorical(self):
256+
"""
257+
Return if the current index type is a categorical type.
258+
259+
Examples
260+
--------
261+
>>> ks.DataFrame({'a': [1]}, index=[1]).index.is_categorical()
262+
False
263+
"""
264+
return is_categorical_dtype(self.dtype)
265+
266+
def is_floating(self):
267+
"""
268+
Return if the current index type is a floating type.
269+
270+
Examples
271+
--------
272+
>>> ks.DataFrame({'a': [1]}, index=[1]).index.is_floating()
273+
False
274+
"""
275+
return is_float_dtype(self.dtype)
276+
277+
def is_integer(self):
278+
"""
279+
Return if the current index type is a integer type.
280+
281+
Examples
282+
--------
283+
>>> ks.DataFrame({'a': [1]}, index=[1]).index.is_integer()
284+
True
285+
"""
286+
return is_integer_dtype(self.dtype)
287+
288+
def is_interval(self):
289+
"""
290+
Return if the current index type is an interval type.
291+
292+
Examples
293+
--------
294+
>>> ks.DataFrame({'a': [1]}, index=[1]).index.is_interval()
295+
False
296+
"""
297+
return is_interval_dtype(self.dtype)
298+
299+
def is_numeric(self):
300+
"""
301+
Return if the current index type is a numeric type.
302+
303+
Examples
304+
--------
305+
>>> ks.DataFrame({'a': [1]}, index=[1]).index.is_numeric()
306+
True
307+
"""
308+
return is_numeric_dtype(self.dtype)
309+
310+
def is_object(self):
311+
"""
312+
Return if the current index type is a object type.
313+
314+
Examples
315+
--------
316+
>>> ks.DataFrame({'a': [1]}, index=["a"]).index.is_object()
317+
True
318+
"""
319+
return is_object_dtype(self.dtype)
320+
242321
def __getattr__(self, item: str) -> Any:
243322
if hasattr(_MissingPandasLikeIndex, item):
244323
property_or_func = getattr(_MissingPandasLikeIndex, item)

databricks/koalas/missing/indexes.py

-14
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,8 @@ class _MissingPandasLikeIndex(object):
7676
insert = unsupported_function('insert')
7777
intersection = unsupported_function('intersection')
7878
is_ = unsupported_function('is_')
79-
is_boolean = unsupported_function('is_boolean')
80-
is_categorical = unsupported_function('is_categorical')
81-
is_floating = unsupported_function('is_floating')
82-
is_integer = unsupported_function('is_integer')
83-
is_interval = unsupported_function('is_interval')
8479
is_lexsorted_for_tuple = unsupported_function('is_lexsorted_for_tuple')
8580
is_mixed = unsupported_function('is_mixed')
86-
is_numeric = unsupported_function('is_numeric')
87-
is_object = unsupported_function('is_object')
8881
is_type_compatible = unsupported_function('is_type_compatible')
8982
join = unsupported_function('join')
9083
map = unsupported_function('map')
@@ -189,16 +182,9 @@ class _MissingPandasLikeMultiIndex(object):
189182
insert = unsupported_function('insert')
190183
intersection = unsupported_function('intersection')
191184
is_ = unsupported_function('is_')
192-
is_boolean = unsupported_function('is_boolean')
193-
is_categorical = unsupported_function('is_categorical')
194-
is_floating = unsupported_function('is_floating')
195-
is_integer = unsupported_function('is_integer')
196-
is_interval = unsupported_function('is_interval')
197185
is_lexsorted = unsupported_function('is_lexsorted')
198186
is_lexsorted_for_tuple = unsupported_function('is_lexsorted_for_tuple')
199187
is_mixed = unsupported_function('is_mixed')
200-
is_numeric = unsupported_function('is_numeric')
201-
is_object = unsupported_function('is_object')
202188
is_type_compatible = unsupported_function('is_type_compatible')
203189
join = unsupported_function('join')
204190
map = unsupported_function('map')

docs/source/reference/indexing.rst

+14
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ Properties
2323
Index.names
2424
Index.empty
2525

26+
Modifying and computations
27+
~~~~~~~~~~~~~~~~~~~~~~~~~~
28+
29+
.. autosummary::
30+
:toctree: api/
31+
32+
Index.is_boolean
33+
Index.is_categorical
34+
Index.is_floating
35+
Index.is_integer
36+
Index.is_interval
37+
Index.is_numeric
38+
Index.is_object
39+
2640
Missing Values
2741
~~~~~~~~~~~~~~
2842
.. autosummary::

0 commit comments

Comments
 (0)