Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
72 changes: 72 additions & 0 deletions databricks/koalas/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,78 @@ def symmetric_difference(self, other, result_name=None, sort=None):

return result

# TODO: return_indexer
def sort_values(self, ascending=True):
"""
Return a sorted copy of the index.

.. note:: This method is not supported for pandas when index has NaN value.
pandas raises unexpected TypeError, but we support treating NaN
as the smallest value.

Parameters
----------
ascending : bool, default True
Should the index values be sorted in an ascending order.

Returns
-------
sorted_index : ks.Index or ks.MultiIndex
Sorted copy of the index.

See Also
--------
Series.sort_values : Sort values of a Series.
DataFrame.sort_values : Sort values in a DataFrame.

Examples
--------
>>> idx = ks.Index([10, 100, 1, 1000])
>>> idx
Int64Index([10, 100, 1, 1000], dtype='int64')

Sort values in ascending order (default behavior).

>>> idx.sort_values()
Int64Index([1, 10, 100, 1000], dtype='int64')

Sort values in descending order.

>>> idx.sort_values(ascending=False)
Int64Index([1000, 100, 10, 1], dtype='int64')

Support for MultiIndex.

>>> kidx = ks.MultiIndex.from_tuples([('a', 'x', 1), ('c', 'y', 2), ('b', 'z', 3)])
>>> kidx # doctest: +SKIP
MultiIndex([('a', 'x', 1),
('c', 'y', 2),
('b', 'z', 3)],
)

>>> kidx.sort_values() # doctest: +SKIP
MultiIndex([('a', 'x', 1),
('b', 'z', 3),
('c', 'y', 2)],
)

>>> kidx.sort_values(ascending=False) # doctest: +SKIP
MultiIndex([('c', 'y', 2),
('b', 'z', 3),
('a', 'x', 1)],
)
"""
sdf = self._internal.sdf
sdf = sdf.orderBy(self._internal.index_scols, ascending=ascending)

internal = _InternalFrame(
sdf=sdf.select(self._internal.index_scols),
index_map=self._kdf._internal.index_map)

result = DataFrame(internal).index

return result

def sort(self, *args, **kwargs):
"""
Use sort_values instead.
Expand Down
2 changes: 2 additions & 0 deletions databricks/koalas/missing/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class _MissingPandasLikeIndex(object):
set_value = unsupported_function('set_value')
slice_indexer = unsupported_function('slice_indexer')
slice_locs = unsupported_function('slice_locs')
sort = unsupported_function('sort')
sort_values = unsupported_function('sort_values')
sortlevel = unsupported_function('sortlevel')
take = unsupported_function('take')
Expand Down Expand Up @@ -173,6 +174,7 @@ class _MissingPandasLikeMultiIndex(object):
set_value = unsupported_function('set_value')
slice_indexer = unsupported_function('slice_indexer')
slice_locs = unsupported_function('slice_locs')
sort = unsupported_function('sort')
sort_values = unsupported_function('sort_values')
sortlevel = unsupported_function('sortlevel')
swaplevel = unsupported_function('swaplevel')
Expand Down
22 changes: 22 additions & 0 deletions databricks/koalas/tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,28 @@ def test_index_fillna(self):
with self.assertRaisesRegex(TypeError, "Unsupported type <class 'list'>"):
kidx.fillna([1, 2])

def test_sort_values(self):
pidx = pd.Index([-10, -100, 200, 100])
kidx = ks.Index([-10, -100, 200, 100])

self.assert_eq(pidx.sort_values(), kidx.sort_values())
self.assert_eq(pidx.sort_values(ascending=False), kidx.sort_values(ascending=False))

pidx.name = 'koalas'
kidx.name = 'koalas'

self.assert_eq(pidx.sort_values(), kidx.sort_values())
self.assert_eq(pidx.sort_values(ascending=False), kidx.sort_values(ascending=False))

pidx = pd.MultiIndex.from_tuples([('a', 'x', 1), ('b', 'y', 2), ('c', 'z', 3)])
kidx = ks.MultiIndex.from_tuples([('a', 'x', 1), ('b', 'y', 2), ('c', 'z', 3)])

pidx.names = ['hello', 'koalas', 'goodbye']
kidx.names = ['hello', 'koalas', 'goodbye']

self.assert_eq(pidx.sort_values(), kidx.sort_values())
self.assert_eq(pidx.sort_values(ascending=False), kidx.sort_values(ascending=False))

def test_index_drop_duplicates(self):
pidx = pd.Index([1, 1, 2])
kidx = ks.Index([1, 1, 2])
Expand Down
7 changes: 7 additions & 0 deletions docs/source/reference/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ Conversion
Index.to_series
Index.to_numpy

Sorting
~~~~~~~
.. autosummary::
:toctree: api/

Index.sort_values

Time-specific operations
~~~~~~~~~~~~~~~~~~~~~~~~
.. autosummary::
Expand Down