-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
API: Index.take inconsistently handle fill_value #12676
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
Closed
Closed
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
|
||
# pylint: disable=E1101,E1103,W0232 | ||
import datetime | ||
import warnings | ||
|
@@ -11,7 +12,7 @@ | |
|
||
from pandas.compat import range, zip, lrange, lzip, map | ||
from pandas import compat | ||
from pandas.core.base import FrozenList | ||
from pandas.core.base import FrozenList, FrozenNDArray | ||
import pandas.core.base as base | ||
from pandas.util.decorators import (Appender, cache_readonly, | ||
deprecate, deprecate_kwarg) | ||
|
@@ -1003,12 +1004,38 @@ def __getitem__(self, key): | |
names=self.names, sortorder=sortorder, | ||
verify_integrity=False) | ||
|
||
def take(self, indexer, axis=None): | ||
indexer = com._ensure_platform_int(indexer) | ||
new_labels = [lab.take(indexer) for lab in self.labels] | ||
return MultiIndex(levels=self.levels, labels=new_labels, | ||
@Appender(_index_shared_docs['take']) | ||
def take(self, indices, axis=0, allow_fill=True, fill_value=None): | ||
indices = com._ensure_platform_int(indices) | ||
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 here |
||
taken = self._assert_take_fillable(self.labels, indices, | ||
allow_fill=allow_fill, | ||
fill_value=fill_value, | ||
na_value=-1) | ||
return MultiIndex(levels=self.levels, labels=taken, | ||
names=self.names, verify_integrity=False) | ||
|
||
def _assert_take_fillable(self, values, indices, allow_fill=True, | ||
fill_value=None, na_value=None): | ||
""" Internal method to handle NA filling of take """ | ||
# only fill if we are passing a non-None fill_value | ||
if allow_fill and fill_value is not None: | ||
if (indices < -1).any(): | ||
msg = ('When allow_fill=True and fill_value is not None, ' | ||
'all indices must be >= -1') | ||
raise ValueError(msg) | ||
taken = [lab.take(indices) for lab in self.labels] | ||
mask = indices == -1 | ||
if mask.any(): | ||
masked = [] | ||
for new_label in taken: | ||
label_values = new_label.values() | ||
label_values[mask] = na_value | ||
masked.append(base.FrozenNDArray(label_values)) | ||
taken = masked | ||
else: | ||
taken = [lab.take(indices) for lab in self.labels] | ||
return taken | ||
|
||
def append(self, other): | ||
""" | ||
Append a collection of Index options together | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
i don't think you need this ensure here (as its inside assert_take_fillabel)?
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.
If we move it to
assert_take_fillable
, it will be executed duplicatelly whentake
has additional logic`` like below. No need to care?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.
yeah I just realized that!
ok the platform_int thing is a whole other issue! (we shouldn't even use them at all), or convert JUST where needed (only for things like take).