Description
In #47836 the *args
/**kwargs
were deprecated in rolling, expanding, and ewm ops. They were used to raise a specific error message to users who tried to use NumPy functions on pandas objects. For example:
ser = Series([1, 2, 3])
np.sum(ser.rolling(2))
raises
pandas.errors.UnsupportedFunctionCall: numpy operations are not valid with window objects. Use .rolling(...).sum() instead
Other Series / DataFrame ops also have kwargs for NumPy compatibility, but unlike the above, the compatibility is to allow the use of NumPy functions to work on pandas objects:
ser = Series([1, 2, 3])
np.sum(ser)
This succeeds with the value 6. If one removes the kwargs on NDFrame.sum, you instead get TypeError: sum() got an unexpected keyword argument 'out'
with the above code.
For sum, the only acceptable calls using kwargs pass a subset of {'dtype': None, 'axis': None, 'keepdims': False, 'initial': None}
. For any key out side of this, or any value that differs from this, we will raise.
Using NumPy functions (especially sum) on pandas objects seems somewhat odd to me, but perhaps there are use cases where one can accomplish a computation that can't be readily done using pandas alone? I've frequently used np.exp(ser)
, but one can use the alternative ser.apply(np.exp)
and to my surprise, the apply version has a little less overhead (but performs the same on Series of 1 million rows or more). Also - even with kwargs removed, np.exp(ser)
will still work, so it doesn't suffice as a useful example.