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

Implement Index.nunique #1132

Merged
merged 9 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
42 changes: 42 additions & 0 deletions databricks/koalas/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,48 @@ def dropna(self):
kdf = DataFrame(internal)
return Index(kdf) if type(self) == Index else MultiIndex(kdf)

def nunique(self, dropna=True):
Copy link
Member

Choose a reason for hiding this comment

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

@RainFung sorry for my late response. There's Series.nunique implementation. Can we move that implementation to IndexOpsMixin to support this in Index?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, i will implement it based on IndexOpsMixin

"""
Return number of unique elements in the object.

Excludes NA values by default.

Parameters
----------
dropna : bool, default True
Don't include NaN in the count.

Returns
-------
int

See Also
--------
DataFrame.nunique: Method nunique for DataFrame.
Series.count: Count non-NA/null observations in the Series.

Examples
--------
>>> idx = ks.Index([1, 1, 2, None])
>>> idx
Float64Index([1.0, 1.0, 2.0, nan], dtype='float64')

>>> idx.nunique()
2

>>> idx.nunique(dropna=False)
3
"""
index_scol = self._internal.index_scols[0]
sdf = self._internal._sdf
if dropna:
result = F.countDistinct(index_scol)
else:
result = (F.countDistinct(index_scol) +
F.when(F.count(F.when(index_scol.isNull(), 1)
.otherwise(None)) >= 1, 1).otherwise(0))
return sdf.select([result]).collect()[0][0]

def unique(self, level=None):
"""
Return unique values in the index.
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 @@ -73,7 +73,6 @@ class _MissingPandasLikeIndex(object):
is_type_compatible = unsupported_function('is_type_compatible')
join = unsupported_function('join')
map = unsupported_function('map')
nunique = unsupported_function('nunique')
RainFung marked this conversation as resolved.
Show resolved Hide resolved
putmask = unsupported_function('putmask')
ravel = unsupported_function('ravel')
reindex = unsupported_function('reindex')
Expand Down
7 changes: 7 additions & 0 deletions databricks/koalas/tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,10 @@ def test_multiindex_isna(self):
NotImplementedError,
"notna is not defined for MultiIndex"):
kidx.notnull()

def test_index_nunique(self):
pidx = pd.DataFrame({'a': ['a', 'b', 'c', 'd']}, index=[1, 1, 2, None]).index
kidx = ks.DataFrame({'a': ['a', 'b', 'c', 'd']}, index=[1, 1, 2, None]).index
RainFung marked this conversation as resolved.
Show resolved Hide resolved

self.assert_eq(pidx.nunique(), kidx.nunique())
self.assert_eq(pidx.nunique(dropna=True), kidx.nunique(dropna=True))
1 change: 1 addition & 0 deletions docs/source/reference/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Modifying and computations
Index.max
Index.rename
Index.unique
Index.nunique
RainFung marked this conversation as resolved.
Show resolved Hide resolved
Index.value_counts

Missing Values
Expand Down