Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
34 changes: 34 additions & 0 deletions databricks/koalas/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,40 @@ def fillna(self, value):
result = DataFrame(self._kdf._internal.copy(sdf=sdf)).index
return result

# TODO: ADD keep parameter
def drop_duplicates(self):
"""
Return Index with duplicate values removed.

Returns
-------
deduplicated : Index

See Also
--------
Series.drop_duplicates : Equivalent method on Series.
DataFrame.drop_duplicates : Equivalent method on DataFrame.

Examples
--------
Generate an pandas.Index with duplicate values.

>>> idx = ks.Index(['lama', 'cow', 'lama', 'beetle', 'lama', 'hippo'])

>>> idx.drop_duplicates() # doctest: +SKIP
Index(['lama', 'cow', 'beetle', 'hippo'], dtype='object')
"""
kdf = self._kdf.copy()
sdf = kdf._internal.sdf.select(self._internal.index_scols).drop_duplicates()
internal = _InternalFrame(sdf=sdf, index_map=self._internal.index_map)
result = DataFrame(internal).index

if isinstance(self, MultiIndex):
result.names = self.names
else:
result.name = self.name
return result

def to_series(self, name: Union[str, Tuple[str, ...]] = None) -> Series:
"""
Create a Series with both index and values equal to the index keys
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 @@ -49,7 +49,6 @@ class _MissingPandasLikeIndex(object):
delete = unsupported_function('delete')
difference = unsupported_function('difference')
drop = unsupported_function('drop')
drop_duplicates = unsupported_function('drop_duplicates')
droplevel = unsupported_function('droplevel')
duplicated = unsupported_function('duplicated')
equals = unsupported_function('equals')
Expand Down Expand Up @@ -137,7 +136,6 @@ class _MissingPandasLikeMultiIndex(object):
delete = unsupported_function('delete')
difference = unsupported_function('difference')
drop = unsupported_function('drop')
drop_duplicates = unsupported_function('drop_duplicates')
droplevel = unsupported_function('droplevel')
duplicated = unsupported_function('duplicated')
equal_levels = unsupported_function('equal_levels')
Expand Down
9 changes: 9 additions & 0 deletions databricks/koalas/tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,12 @@ def test_index_fillna(self):

with self.assertRaisesRegex(TypeError, "Unsupported type <class 'list'>"):
kidx.fillna([1, 2])

def test_index_drop_duplicates(self):
pidx = pd.Index([1, 1, 2])
kidx = ks.Index([1, 1, 2])
self.assert_eq(pidx.drop_duplicates(), kidx.drop_duplicates())

pidx = pd.MultiIndex.from_tuples([(1, 1), (1, 1), (2, 2)], names=['level1', 'level2'])
kidx = ks.MultiIndex.from_tuples([(1, 1), (1, 1), (2, 2)], names=['level1', 'level2'])
self.assert_eq(pidx.drop_duplicates(), kidx.drop_duplicates())
1 change: 1 addition & 0 deletions docs/source/reference/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Modifying and computations
Index.is_interval
Index.is_numeric
Index.is_object
Index.drop_duplicates
Copy link
Member

Choose a reason for hiding this comment

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

Sorry, can you add drop_duplicates at MultiIndex too?

Copy link
Contributor Author

@RainFung RainFung Dec 17, 2019

Choose a reason for hiding this comment

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

MultiIndex.drop_duplicates has been deprecated in pandas 0.26 doc.https://dev.pandas.io/docs/reference/indexing.html

Copy link
Member

Choose a reason for hiding this comment

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

Nice!

Index.value_counts

Missing Values
Expand Down