-
-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
ENH: allow storing ExtensionArrays in the Index #34159
Closed
Closed
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fb6d13c
ENH: allow storing ExtensionArrays in the Index
jorisvandenbossche cd917b1
use extract_array
jorisvandenbossche 8829aba
Merge remote-tracking branch 'upstream/master' into EA-index
jorisvandenbossche 099e73a
fixup merge
jorisvandenbossche 7a9699c
clean-up
jorisvandenbossche File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -346,11 +346,13 @@ def __new__( | |
ea_cls = dtype.construct_array_type() | ||
data = ea_cls._from_sequence(data, dtype=dtype, copy=False) | ||
else: | ||
data = np.asarray(data, dtype=object) | ||
# TODO clean-up with extract_array ? | ||
if isinstance(data, Index): | ||
data = data._data | ||
elif isinstance(data, ABCSeries): | ||
data = data.array | ||
|
||
# coerce to the object dtype | ||
data = data.astype(object) | ||
return Index(data, dtype=object, copy=copy, name=name, **kwargs) | ||
return cls._simple_new(data, name) | ||
|
||
# index-like | ||
elif isinstance(data, (np.ndarray, Index, ABCSeries)): | ||
|
@@ -458,7 +460,7 @@ def _simple_new(cls, values, name: Label = None): | |
|
||
Must be careful not to recurse. | ||
""" | ||
assert isinstance(values, np.ndarray), type(values) | ||
assert isinstance(values, (np.ndarray, ExtensionArray)), type(values) | ||
|
||
result = object.__new__(cls) | ||
result._data = values | ||
|
@@ -2126,6 +2128,8 @@ def fillna(self, value=None, downcast=None): | |
Series.fillna : Fill NaN Values of a Series. | ||
""" | ||
self._assert_can_do_op(value) | ||
if is_extension_array_dtype(self.dtype): | ||
return self._shallow_copy(self._values.fillna(value)) | ||
if self.hasnans: | ||
result = self.putmask(self._isnan, value) | ||
if downcast is None: | ||
|
@@ -2525,7 +2529,9 @@ def _union(self, other, sort): | |
# worth making this faster? a very unusual case | ||
value_set = set(lvals) | ||
result.extend([x for x in rvals if x not in value_set]) | ||
result = Index(result)._values # do type inference here | ||
result = Index( | ||
result, dtype=self.dtype | ||
)._values # do type inference here | ||
else: | ||
# find indexes of things in "other" that are not in "self" | ||
if self.is_unique: | ||
|
@@ -3797,7 +3803,7 @@ def values(self) -> np.ndarray: | |
Index.array : Reference to the underlying data. | ||
Index.to_numpy : A NumPy array representing the underlying data. | ||
""" | ||
return self._data.view(np.ndarray) | ||
return self._data # .view(np.ndarray) | ||
|
||
@cache_readonly | ||
@doc(IndexOpsMixin.array) | ||
|
@@ -3839,7 +3845,10 @@ def _get_engine_target(self) -> np.ndarray: | |
""" | ||
Get the ndarray that we can pass to the IndexEngine constructor. | ||
""" | ||
return self._values | ||
if isinstance(self._values, np.ndarray): | ||
return self._values | ||
else: | ||
return np.asarray(self._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. doesnt |
||
|
||
@doc(IndexOpsMixin.memory_usage) | ||
def memory_usage(self, deep: bool = False) -> int: | ||
|
@@ -4232,10 +4241,18 @@ def equals(self, other: Any) -> bool: | |
# d-level MultiIndex can equal d-tuple Index | ||
return other.equals(self) | ||
|
||
if is_extension_array_dtype(other.dtype): | ||
if is_extension_array_dtype(other.dtype) and type(other) != Index: | ||
# All EA-backed Index subclasses override equals | ||
return other.equals(self) | ||
|
||
if is_extension_array_dtype(self.dtype): | ||
if is_object_dtype(other.dtype): | ||
try: | ||
other = other.astype(self.dtype) | ||
except Exception: | ||
return False | ||
return self._values.equals(other._values) | ||
|
||
return array_equivalent(self._values, other._values) | ||
|
||
def identical(self, other) -> bool: | ||
|
@@ -4759,6 +4776,15 @@ def map(self, mapper, na_action=None): | |
|
||
attributes = self._get_attributes_dict() | ||
|
||
if is_extension_array_dtype(self.dtype): | ||
# try to coerce back to original dtype | ||
# TODO this should use a strict version | ||
try: | ||
# TODO use existing helper method for this | ||
new_values = self._values._from_sequence(new_values, dtype=self.dtype) | ||
except Exception: | ||
pass | ||
|
||
# we can return a MultiIndex | ||
if new_values.size and isinstance(new_values[0], tuple): | ||
if isinstance(self, MultiIndex): | ||
|
@@ -5193,7 +5219,10 @@ def delete(self, loc): | |
>>> idx.delete([0, 2]) | ||
Index(['b'], dtype='object') | ||
""" | ||
return self._shallow_copy(np.delete(self._data, loc)) | ||
# this is currently overriden by EA-based Index subclasses | ||
keep = np.ones(len(self), dtype=bool) | ||
keep[loc] = False | ||
return self._shallow_copy(self._data[keep]) | ||
|
||
def insert(self, loc: int, item): | ||
""" | ||
|
@@ -5212,9 +5241,14 @@ def insert(self, loc: int, item): | |
""" | ||
# Note: this method is overridden by all ExtensionIndex subclasses, | ||
# so self is never backed by an EA. | ||
arr = np.asarray(self) | ||
item = self._coerce_scalar_to_index(item)._values | ||
idx = np.concatenate((arr[:loc], item, arr[loc:])) | ||
|
||
if is_extension_array_dtype(self.dtype): | ||
arr = self._values | ||
idx = arr._concat_same_type([arr[:loc], item, arr[loc:]]) | ||
else: | ||
arr = np.asarray(self) | ||
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. arr = self._values could go outside the if/elif |
||
idx = np.concatenate((arr[:loc], item, arr[loc:])) | ||
return Index(idx, name=self.name) | ||
|
||
def drop(self, labels, errors: str_t = "raise"): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1