-
-
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?
Conversation
Plan to run a full set of ASVs next week, some microbenchmarks from pandas.core.dtypes.cast import maybe_unbox_numpy_scalar
with pd.option_context("python_scalars", True):
%timeit maybe_unbox_numpy_scalar(np.int64(2))
# 828 ns ± 9.91 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
%timeit maybe_unbox_numpy_scalar(2)
# 161 ns ± 0.414 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
ser = pd.Series([1, 2, 3] * 10_000)
with pd.option_context("python_scalars", True):
%timeit ser.sum()
# 9.42 μs ± 423 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
with pd.option_context("python_scalars", False):
%timeit ser.sum()
# 8.28 μs ± 137 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each) |
| if using_python_scalars() and isinstance(value, np.generic): | ||
| if isinstance(result, np.longdouble): | ||
| result = float(result) |
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.
What if value is np.int ? Don't you want to handle that differently?
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 give an example input to this function that you think gives an undesirable result.
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.
` np.array([1,2,3]).sum()` returns `np.int64(6)`, so wouldn't `maybe_unbox_numpy_scalar(value)` need to return `6` instead of `6.0` if `value` was set to `np.int64(6)` ?
doc/source/whatsnew/vX.X.X.rstfile if fixing a bug or adding a new feature.Adds an experimental option to return Python scalars instead of NumPy scalars across the API. This is not yet fully implemented everywhere, e.g.
Series.__getitem__, but I'm hoping reductions are a substantial chunk.This is complicated by #62988 where it was found that many of our doctests are not running. We run those doctests using NumPy>=2, and if we were to get those doctests to pass as-is, we would need to change the NumPy reprs from e.g.
2tonp.int64(2). If we then change reductions et al to returning Python scalars, we'd then change all the reprs back from e.g.np.int64(2)to2. So instead I think we can:future.python_scalarstoTruein 4.0, deprecate the future option.