-
-
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
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,16 @@ 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
|
||
| 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" | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -171,7 +171,7 @@ def test_floating_array_numpy_sum(values, expected): | |
|
|
||
|
|
||
| @pytest.mark.parametrize("op", ["sum", "min", "max", "prod"]) | ||
| def test_preserve_dtypes(op): | ||
| def test_preserve_dtypes(op, using_python_scalars): | ||
| df = pd.DataFrame( | ||
| { | ||
| "A": ["a", "b", "b"], | ||
|
|
@@ -182,7 +182,10 @@ def test_preserve_dtypes(op): | |
|
|
||
| # op | ||
| result = getattr(df.C, op)() | ||
| assert isinstance(result, np.float64) | ||
| if using_python_scalars: | ||
| assert isinstance(result, float) | ||
|
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 think float64 subclasses float, so this won't exclude float64 |
||
| else: | ||
| assert isinstance(result, np.float64) | ||
|
|
||
| # groupby | ||
| result = getattr(df.groupby("A"), op)() | ||
|
|
||
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.