-
-
Notifications
You must be signed in to change notification settings - Fork 19.3k
ENH: Add future.python_scalars #63016
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
base: main
Are you sure you want to change the base?
Changes from all commits
477cc4f
bd953a2
0896a2f
64de4aa
1de3610
fef7950
919536d
bac5764
4902463
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
|
|
||
| from pandas._config import ( | ||
| is_nan_na, | ||
| using_python_scalars, | ||
| using_string_dtype, | ||
| ) | ||
|
|
||
|
|
@@ -1437,6 +1438,22 @@ def construct_1d_arraylike_from_scalar( | |
| return subarr | ||
|
|
||
|
|
||
| def maybe_unbox_numpy_scalar(value): | ||
| result = value | ||
| if using_python_scalars() and isinstance(value, np.generic): | ||
| if isinstance(result, np.longdouble): | ||
| result = float(result) | ||
Dr-Irv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| elif isinstance(result, np.complex256): | ||
| result = complex(result) | ||
| elif isinstance(result, np.datetime64): | ||
| result = Timestamp(result) | ||
| elif isinstance(result, np.timedelta64): | ||
| result = Timedelta(result) | ||
| else: | ||
| result = value.item() | ||
|
Member
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. I know this will mess up on a timedelta64: I don't know if there are other cases where
Member
Author
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. Thanks - will add a test for all dtypes. Here is the full list of scalars and their corresponding item type without datetime/timedelta. Only other problematic one is complex256. For datetime/timedelta, the arrow dtypes already return |
||
| return result | ||
|
|
||
|
|
||
| def _maybe_box_and_unbox_datetimelike(value: Scalar, dtype: DtypeObj): | ||
| # Caller is responsible for checking dtype.kind in "mM" | ||
|
|
||
|
|
||
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.
why doing this instead of maybe_unbox_numpy_scalar?
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.
The
resulthere prior to L1543 is already a Python scalar whenfuture.python_scalars=Truedue to calling the reduction function. In this block,keepdims=Trueso we need to convert it to a NumPy array.