-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
API: Change default for Index.union sort #25007
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 8 commits
aac172c
d4bcc55
45c827c
68b72a6
8716f97
f7056d5
e82cbb1
2a2de25
5c3da74
ce6d1db
52a2f2f
bb848f1
b15dc7e
27b5b16
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 |
---|---|---|
|
@@ -15,10 +15,34 @@ Whats New in 0.24.1 (February XX, 2019) | |
These are the changes in pandas 0.24.1. See :ref:`release` for a full changelog | ||
including other versions of pandas. | ||
|
||
.. _whatsnew_0241.api: | ||
|
||
API Changes | ||
~~~~~~~~~~~ | ||
|
||
Changing the ``sort`` parameter for :meth:`Index.Union` | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
The default ``sort`` value for :meth:`Index.union` has changed from ``True`` to ``None``. | ||
The default *behavior* remains the same: The result is sorted, unless | ||
|
||
1. ``self`` and ``other`` are identical | ||
2. ``self`` or ``other`` is empty | ||
3. ``self`` or ``other`` contain values that can not be compared (a ``RuntimeWarning`` is raised). | ||
|
||
This allows ``sort=True`` to now mean "always sort" A ``TypeError`` is raised if the values cannot be compared. | ||
|
||
Changed the behavior of :meth:`Index.intersection` with ``sort=True`` | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
When ``sort=True`` is provided to :meth:`Index.intersection`, the values are always sorted. In 0.24.0, | ||
the values would not be sorted when ``self`` and ``other`` were identical. Pass ``sort=False`` to not | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am -1 on this change. We do NOT do this elsewhere, e.g. |
||
sort the values. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should make it clearer that this was new behaviour in 0.24.0, and that no behaviour changed compared to what you could do on 0.23 ? |
||
|
||
.. _whatsnew_0241.regressions: | ||
|
||
Fixed Regressions | ||
^^^^^^^^^^^^^^^^^ | ||
~~~~~~~~~~~~~~~~~ | ||
|
||
- Bug in :meth:`DataFrame.itertuples` with ``records`` orient raising an ``AttributeError`` when the ``DataFrame`` contained more than 255 columns (:issue:`24939`) | ||
- Bug in :meth:`DataFrame.itertuples` orient converting integer column names to strings prepended with an underscore (:issue:`24940`) | ||
|
@@ -27,7 +51,7 @@ Fixed Regressions | |
.. _whatsnew_0241.enhancements: | ||
|
||
Enhancements | ||
^^^^^^^^^^^^ | ||
~~~~~~~~~~~~ | ||
|
||
|
||
.. _whatsnew_0241.bug_fixes: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2245,18 +2245,34 @@ def _get_reconciled_name_object(self, other): | |
return self._shallow_copy(name=name) | ||
return self | ||
|
||
def union(self, other, sort=True): | ||
def union(self, other, sort=None): | ||
""" | ||
Form the union of two Index objects. | ||
|
||
Parameters | ||
---------- | ||
other : Index or array-like | ||
sort : bool, default True | ||
Sort the resulting index if possible | ||
sort : bool or None, default None | ||
Whether to sort the resulting Index. | ||
|
||
* None : Sort the result, except when | ||
|
||
1. `self` and `other` are equal. | ||
2. `self` or `other` has length 0. | ||
3. Some values in `self` or `other` cannot be compared. | ||
A RuntimeWarning is issued in this case. | ||
|
||
* True : sort the result. A TypeError is raised when the | ||
values cannot be compared. | ||
* False : do not sort the result. | ||
|
||
.. versionadded:: 0.24.0 | ||
|
||
.. versionchanged:: 0.24.1 | ||
|
||
Changed the default `sort` to None, matching the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this being changed? this is certainly not a regression at all. This was the default behavior. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be clear: no behaviour is changed. It was indeed the default, it stays the default. It's only the value that encodes the default that is changed (True -> None), so that True can mean something else (=always sort). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, maybe it should be more clear in the doc-string |
||
behavior of pandas 0.23.4 and earlier. | ||
|
||
Returns | ||
------- | ||
union : Index | ||
|
@@ -2273,10 +2289,16 @@ def union(self, other, sort=True): | |
other = ensure_index(other) | ||
|
||
if len(other) == 0 or self.equals(other): | ||
return self._get_reconciled_name_object(other) | ||
result = self._get_reconciled_name_object(other) | ||
if sort: | ||
result = result.sort_values() | ||
return result | ||
|
||
if len(self) == 0: | ||
return other._get_reconciled_name_object(self) | ||
result = other._get_reconciled_name_object(self) | ||
if sort: | ||
result = result.sort_values() | ||
return result | ||
|
||
# TODO: is_dtype_union_equal is a hack around | ||
# 1. buggy set ops with duplicates (GH #13432) | ||
|
@@ -2319,13 +2341,16 @@ def union(self, other, sort=True): | |
else: | ||
result = lvals | ||
|
||
if sort: | ||
if sort is None: | ||
try: | ||
result = sorting.safe_sort(result) | ||
except TypeError as e: | ||
warnings.warn("{}, sort order is undefined for " | ||
"incomparable objects".format(e), | ||
RuntimeWarning, stacklevel=3) | ||
elif sort: | ||
# raise if not sortable. | ||
result = sorting.safe_sort(result) | ||
|
||
# for subclasses | ||
return self._wrap_setop_result(other, result) | ||
|
@@ -2342,8 +2367,12 @@ def intersection(self, other, sort=False): | |
Parameters | ||
---------- | ||
other : Index or array-like | ||
sort : bool, default False | ||
Sort the resulting index if possible | ||
sort : bool or None, default False | ||
Whether to sort the resulting index. | ||
|
||
* False : do not sort the result. | ||
* True : sort the result. A TypeError is raised when the | ||
values cannot be compared. | ||
|
||
.. versionadded:: 0.24.0 | ||
|
||
|
@@ -2367,7 +2396,10 @@ def intersection(self, other, sort=False): | |
other = ensure_index(other) | ||
|
||
if self.equals(other): | ||
return self._get_reconciled_name_object(other) | ||
result = self._get_reconciled_name_object(other) | ||
if sort: | ||
result = result.sort_values() | ||
return result | ||
|
||
if not is_dtype_equal(self.dtype, other.dtype): | ||
this = self.astype('O') | ||
|
Uh oh!
There was an error while loading. Please reload this page.