-
-
Notifications
You must be signed in to change notification settings - Fork 18k
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
API: consider undeprecating Series.item() ? #29250
Comments
could a errors kwarg to Series.squeeze achieve this? |
Yeah, I actually used For the actual suggestion: this might get complicated, as in |
agreed that an additional argument would need to be consistent with DataFrame.squeeze and yet not too complicated. rational for this would be to avoid two ways of doing the same operation, with the difference being that one raises. |
Intuitively I thought It seems like
yields I feel like this was possible before, so it was probably deprecated for a good reason that I'm not aware of. But if we're talking about un-deprecation, I think this should be considered as well. Sorry if I open a closed discussion with that 😄 |
I'm fine with un-deprecating this. Is it a blocker for 1.0? Would we add it to DataFrame, with a similar behavior to a 2D ndarray? |
It's certainly not a blocker, but since it's deprecated, and if we want to do this, better sooner than later (and it should be a quick PR).
That could be done yes, although I personally find that less needed (boolean indexing on the columns is much less common I think)
I don't think |
The other use case for NumPy's |
For that use case, I suppose you would want to keep the "full" behaviour of numpy? (so also |
I don't think it's really needed to support the full form of .item(i) with
an argument. That version is definitely redundant with indexing.
…On Fri, Nov 15, 2019 at 4:11 AM Joris Van den Bossche < ***@***.***> wrote:
For that use case, I suppose you would want to keep the "full" behaviour
of numpy? (so also s.item(i) , while for the above discussed use case
(getting rid of the Series container for len-1 Series), s.item() without
argument is enough).
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#29250?email_source=notifications&email_token=AAJJFVV45BKH7H3ZHPALWHLQTZRUNA5CNFSM4JFWTTT2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEEEZOWQ#issuecomment-554276698>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAJJFVWXNBDLB7D5UB7UYW3QTZRUNANCNFSM4JFWTTTQ>
.
|
Since `Index.item()` is un-deprecated from pandas 1.0.0 (pandas-dev/pandas#29250), this PR proposes `Index.item()` and `MultiIndex.item()`. ```python >>> kidx = ks.Index([10]) >>> kidx.item() 10 >>> kmidx = ks.MultiIndex.from_tuples([('a', 'x')]) >>> kmidx.item() ('a', 'x') ```
Since `Index.item()` is un-deprecated from pandas 1.0.0 (pandas-dev/pandas#29250), this PR proposes `Index.item()` and `MultiIndex.item()`. ```python >>> kidx = ks.Index([10]) >>> kidx.item() 10 >>> kmidx = ks.MultiIndex.from_tuples([('a', 'x')]) >>> kmidx.item() ('a', 'x') ```
I recently ran into this as well (but forgot to open an issue), and raised now by @alexiswl in #18262 (comment)
Series.item()
was a consequence of (historically) inheriting from np.ndarray, and was deprecated (like a set of other ndarray-inhertited methods/attributes) a while ago.While
.item()
could also be used to select the "i-th" element (.item(i)
), and this use case is certainly redundant (not arguing here to get that aspect back), there is one use case whereitem()
can actually be useful: if you do not pass i, the method returns the element of the Series only if it has one element, otherwise it errors.Such a situation can typically occur if you use boolean indexing (or
query
) to select a single element. Eg in cases likes[s == 'val']
ordf.loc[df['col1'] == 'val', 'col2']
where you know the condition should yield a single element.You then typically want the scalar element as result, but those two code snippets give you a Series of one element. In those cases, you could use
item()
to retrieve it:s[s == 'val'].item()
.I saw some people using
.item()
exactly for this use case, so wondering if it is worth to keep it for this (only the version without passing i).The logical alternative is doing a
.iloc[0]
, but.item()
has the advantage of guaranteeing there was actually only one result item.The text was updated successfully, but these errors were encountered: