-
-
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
Conversation
@sinhrks as an aside (maybe a new issue). I think we should remove |
actually, forget what I said above. The |
@@ -29,12 +29,16 @@ New features | |||
Enhancements | |||
~~~~~~~~~~~~ | |||
|
|||
- ``Index.take`` now handles ``allow_fill`` and ``fill_value`` consistently (:issue:`12631`) |
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.
Current (updated in this PR) docstring says as below, is it not enough?
If allow_fill=True and fill_value is not None, indices specified by -1 is regarded as NA. If Index doesn't hold NA, raise ValueError
c7af0dc
to
97dd513
Compare
|
||
if self._can_hold_na: | ||
# only fill if we are passing a non-None fill_value | ||
if allow_fill and fill_value is not None: |
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.
Actually I think we need to either make both this, and tseries/base
take:
either: raise on any negative indexers except -1
or: make a new method to do this, maybe .take_with_fill
and restore .take
for compat with 'regular' take. That will raise on indices that are not >= -1. These are serving double duty here.
moving this comment to main: Actually I think we need to either make both either: raise on any negative indexers except -1 |
To be compat with
|
ok if we change the default to So |
@jreback Based on the current impl, I don't think we should change the default to
Because current default is |
@sinhrks hmm ok, though I think we should have validation though (e.g. if you try something like
[10] is wrong, this is treating -2 as a positional taker, and -1 as a NA indicator. so ok with leaving the API, just want to put in some validation tests so that if we are NA filling, then don't allow negative values (except -1) |
0644232
to
17cd4ee
Compare
Yes, added a logic to raise Indexer contains outside of bounds raises |
if mask.any(): | ||
taken[mask] = self._na_value | ||
else: | ||
if allow_fill and fill_value is not None: |
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.
can you refactor this function, something like (as an internal function), as repeating this several times.
def _assert_take_fillable(self, indices, allow_fill, fill_value):
if allow_fill and fill_value:
if (indices < -1).any():
raise .....
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.
Sure, how about this?
|
@jreback You mean always raising
|
no when in filling mode it should raise ValueError with negative indices ; in 12, the -5 should be caught before the take is done (as its negative and not -1) |
Thanks, understood. Fixed and added tests. |
taken = self.codes.take(indexer) | ||
@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 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 when take
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).
@sinhrks thanks! you are fixing some of these internal warts! keep em comin' |
related to #4400 Added more tests for sparse indexing. `SparseArray.take`` has optimized logic to omit dense ``np.ndarray`` creation. SparseSeires.iloc` can work with negative indices. Made ``SparseArray.take`` to handle negative indices as the same rule as ``Index`` (#12676) Author: sinhrks <sinhrks@gmail.com> Closes #12796 from sinhrks/sparse_test_at and squashes the following commits: df1f056 [sinhrks] ENH/PERF SparseArray.take indexing
git diff upstream/master | flake8 --diff