Skip to content
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

BUG+DEPR: undeprecate item, fix dt64/td64 output type #30175

Merged
merged 10 commits into from
Dec 18, 2019

Conversation

jbrockmendel
Copy link
Member

@jbrockmendel jbrockmendel commented Dec 10, 2019

Does the un-deprecation need a dedicated discussion, or is there consensus on that?

I'd be happy to split this into separate bug/depr pieces of reviewers prefer.

stacklevel=2,
)
return self.values.item()
if len(self) == 1:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was adding this condition discussed somewhere? I would have thought just keep existing behaviour

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the bugfix part. dt64, dt64tz, and td64 we're currently incorrectly returning int

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm doesn't this break non-DTA though?

>>> type(pd.Series(range(1)).item())
<class 'int'>
>>> type(pd.Series(range(1))[0])
<class 'numpy.int64'>

I thought one of the points of item was to return a Python object (at least in the Numpy world)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ current behavior

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think we should keep the behaviour of item to return a python scalar (where possible of course, so for datetime/timedelta it is fine to return a pandas Timestamp/Timedelta I think)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, will update.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has this been resolved?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the concerns raised by @WillAyd and @jorisvandenbossche have been addressed

@jorisvandenbossche
Copy link
Member

This issue is #29250

if not needs_i8_conversion(self.dtype):
# numpy returns ints instead of datetime64/timedelta64 objects,
# which we need to wrap in Timestamp/Timedelta/Period regardless.
return self.values.item()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work for ExtensionArrays. We can discuss adding item to the interface, but I would rather (or at least for now) let ExtensionArrays take the path you have below that uses iteration (which should already handle the conversion to a python scalar)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be +1 on adding to EA arrays, why have inconsistency in code paths.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work for ExtensionArrays

This uses .values, so will convert to ndarray and then call item. So it shouldn't be any more broken than what we have now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have no objection to adding item to EAs separately

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it shouldn't be any more broken than what we have now.

And to fix that, the only thing that is needed is adding a and not is_extension_array_dtype(self.dtype): to the above if check.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

im happy to do that here, will need tests in a follow-up

@jreback jreback added the Deprecate Functionality to remove in pandas label Dec 11, 2019
if not needs_i8_conversion(self.dtype):
# numpy returns ints instead of datetime64/timedelta64 objects,
# which we need to wrap in Timestamp/Timedelta/Period regardless.
return self.values.item()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be +1 on adding to EA arrays, why have inconsistency in code paths.

@jreback jreback added this to the 1.0 milestone Dec 11, 2019
Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, question

)
return self.values.item()
if not (
is_extension_array_dtype(self.dtype) or needs_i8_conversion(self.dtype)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this redundant? as all needs_i8_conversion are already EA

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, dt64 and td64 are need i8 conversion but are not EA

@jreback
Copy link
Contributor

jreback commented Dec 15, 2019

also needs a rebase

@TomAugspurger
Copy link
Contributor

@jorisvandenbossche jorisvandenbossche merged commit 924f839 into pandas-dev:master Dec 18, 2019
@jorisvandenbossche
Copy link
Member

Thanks @jbrockmendel !

(will look into adding a EA test)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Deprecate Functionality to remove in pandas
Projects
None yet
Development

Successfully merging this pull request may close these issues.

API: consider undeprecating Series.item() ?
6 participants