-
Notifications
You must be signed in to change notification settings - Fork 367
Implement sort_values for Index/MultiIndex #1120
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
Changes from 1 commit
4f649e6
25aca52
9d75621
8ae7722
ef14c37
7e717cc
a8dda7d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -711,6 +711,83 @@ 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._internal.index_map) | ||
|
|
||
| result = DataFrame(internal).index | ||
|
|
||
| if isinstance(self, MultiIndex): | ||
| result.names = self.names | ||
| else: | ||
| result.name = self.name | ||
|
||
|
|
||
| return result | ||
|
|
||
| def __getattr__(self, item: str) -> Any: | ||
| if hasattr(_MissingPandasLikeIndex, item): | ||
| property_or_func = getattr(_MissingPandasLikeIndex, item) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.