-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
ENH - Index set operation modifications to address issue #23525 #23538
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
c2cf269
435e50f
4922fd3
5e528a1
cdaa5b0
40d57ec
11fd041
8f0ace3
8364c2e
ab329a9
93486ad
e435e4c
b9787b8
2241b65
4daf360
6e5a52b
d344e11
b339bd1
85e2db7
cf34960
7150c22
fbb3743
5aa41f6
02d7a3b
558e182
706f973
2ccab59
c70f1c0
edb7e9c
84bfbda
aba75fe
fc9f138
69cce99
fdfc7d7
42ca70e
5b25645
9b1ee7f
fdf9b71
1de3cc8
8ed1093
77ca3a3
5921038
3b94e3b
fd4510e
345eec1
265a7ee
5de3d57
6d82621
0af8a24
5a87715
a4f9e78
c3c0caa
0bcbdf4
c410625
6bb054f
aea731c
b5938fc
25452fc
0b97a79
bf11c6f
6fd941d
32037b5
8870006
1d12bc9
92f6707
fbf3242
38d9f74
b9e7b18
69aaa93
b57160a
daa1287
54898c1
fa839a9
a36f475
b840f49
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2756,6 +2756,15 @@ def _get_reconciled_name_object(self, other): | |
return self._shallow_copy(name=name) | ||
return self | ||
|
||
def _union_inconsistent_dtypes(self, other): | ||
this = self.astype('O') | ||
other = Index(other).astype('O') | ||
return Index.union(this, other).astype('O') | ||
|
||
def _is_inconsistent(self, other): | ||
ms7463 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return (type(self) is not type(other) | ||
or not is_dtype_equal(self.dtype, other.dtype)) | ||
|
||
def union(self, other): | ||
""" | ||
Form the union of two Index objects and sorts if possible. | ||
|
@@ -2780,27 +2789,30 @@ def union(self, other): | |
self._assert_can_do_setop(other) | ||
other = ensure_index(other) | ||
|
||
if self.equals(other): | ||
if self._is_inconsistent(other): | ||
return self._union_inconsistent_dtypes(other) | ||
|
||
# if is_dtype_equal(self.dtype, other.dtype): | ||
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. can you put some comments here on the decisions here? 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. All subclass implementations started with this check, so thought it would make sense to pull this out to be common among all, and implement index specific overriden behavior in the |
||
if len(self) == 0: | ||
return other._get_reconciled_name_object(self) | ||
elif len(other) == 0: | ||
return self._get_reconciled_name_object(other) | ||
|
||
# Don't allow empty index to move on. If `other` is not monotonic-incr | ||
# ...will fail | ||
if is_dtype_equal(self.dtype, other.dtype): | ||
if len(self) == 0: | ||
return other._get_reconciled_name_object(self) | ||
elif len(other) == 0: | ||
return self._get_reconciled_name_object(other) | ||
if self.equals(other): | ||
return self._get_reconciled_name_object(other) | ||
|
||
|
||
# TODO: is_dtype_union_equal is a hack around | ||
# 1. buggy set ops with duplicates (GH #13432) | ||
# 2. CategoricalIndex lacking setops (GH #10186) | ||
# Once those are fixed, this workaround can be removed | ||
if not is_dtype_union_equal(self.dtype, other.dtype): | ||
this = self.astype('O') | ||
other = other.astype('O') | ||
# force object dtype, for empty index cases | ||
return this.union(other).astype('O') | ||
|
||
# remove? this might be too lenient | ||
# if not is_dtype_union_equal(self.dtype, other.dtype): | ||
# this = self.astype('O') | ||
# other = other.astype('O') | ||
# # force object dtype, for empty index cases | ||
# return this.union(other).astype('O') | ||
|
||
# TODO(EA): setops-refactor, clean all this up | ||
if is_period_dtype(self) or is_datetime64tz_dtype(self): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -228,6 +228,17 @@ def _assert_safe_casting(cls, data, subarr): | |
raise TypeError('Unsafe NumPy casting, you must ' | ||
'explicitly cast') | ||
|
||
def _is_inconsistent(self, other): | ||
from pandas.core.indexes.range import RangeIndex | ||
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. use ABCRangeIndex |
||
is_inconsistent = super(Int64Index, self)._is_inconsistent(other) | ||
if is_inconsistent: | ||
if type(self) is Int64Index and isinstance(other, RangeIndex): | ||
return False | ||
else: | ||
return True | ||
else: | ||
return is_inconsistent | ||
|
||
|
||
Int64Index._add_numeric_methods() | ||
Int64Index._add_logical_methods() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -416,6 +416,13 @@ def _extended_gcd(self, a, b): | |
old_t, t = t, old_t - quotient * t | ||
return old_r, old_s, old_t | ||
|
||
def _is_inconsistent(self, other): | ||
ms7463 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
is_inconsistent = super(RangeIndex, self)._is_inconsistent(other) | ||
if is_inconsistent: | ||
return not isinstance(other, Int64Index) | ||
else: | ||
return is_inconsistent | ||
|
||
def union(self, other): | ||
""" | ||
Form the union of two Index objects and sorts if possible | ||
|
@@ -429,6 +436,10 @@ def union(self, other): | |
union : Index | ||
""" | ||
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. is this needed any longer? 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. see related comment in DatetimeIndex module |
||
self._assert_can_do_setop(other) | ||
|
||
if self._is_inconsistent(other): | ||
return self._union_inconsistent_dtypes(other) | ||
|
||
if len(other) == 0 or self.equals(other) or len(self) == 0: | ||
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. same comment as above |
||
return super(RangeIndex, self).union(other) | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.