Feature Type
-
[X] Adding new functionality to pandas
-
[ ] Changing existing functionality in pandas
-
[ ] Removing existing functionality in pandas
Problem Description
Could we add a .exp
method (in the spirit of .add
, .sub
, .pow
methods)? By default would use np.exp(1)
as the base. But could take base
as an additional argument.
Feature Description
Add "exponentiation" method for Series and DataFrames
Alternative Solutions
equivalent of ser.apply(np.exp)
Additional Context
No response
Comment From: jrmylow
i'm happy to take this unless there is a good reason against this feature
Comment From: jrmylow
take
Comment From: mroeschke
Thanks for the request but I would be -1 on this feature so far as np.exp
can already accept DataFrame and Series objects and pandas is trying to minimize redundant ways to accomplish the same task:
In [1]: import numpy as np
In [2]: import pandas as pd
np.exp
In [3]: np.exp(pd.DataFrame(np.eye(3)))
Out[3]:
0 1 2
0 2.718282 1.000000 1.000000
1 1.000000 2.718282 1.000000
2 1.000000 1.000000 2.718282
In [4]: np.exp(pd.Series([1, 2]))
Out[4]:
0 2.718282
1 7.389056
dtype: float64
Comment From: 8one6
@mroeschke , I appreciate your take on this, but I lean the other way.
If I have a series and I want to exponentiate it, I would save a few keystrokes with ser.exp()
compared with np.exp(ser)
.
But more important than the "keystroke savings" is the "flow" you get from chaining similar steps with similar syntax. I strongly prefer ser.sub(1).mul(0.5).exp()
to np.exp(ser.sub(1).mul(0.5))
. The three steps here feel similar to me intuitively, and so I feel like the "correct" solution should let me chain them with similar syntax.
Regarding your comment about trying to avoid multiple ways to do the same thing, we have (and I would want to keep):
ser + 1
ser.add(1)
ser ** 2
ser.pow(2)
etc. There are lots of places where chaining a number of steps this way is readable and intuitive. I'd love to be able to hit exp
in the same way.
Comment From: mroeschke
Thanks for the feedback. While I see the flow argument, the project has been a bit weary on expanding on the large API of pandas, especially for functionality that is stylistic with known alternatives (pandas defines __array_ufunc__
to allow any numpy mathematical function (ufunc))
Comment From: 8one6
Sigh. Feels like a weird "half-done" current state at the moment: add
, sub
, mul
, div
, pow
...but poor old exp
left out in the cold.
Comment From: 8one6
Last pitch: the methods I mention above all do optional alignment on a level of the index. So you can do ser.add(other_ser, level=my_level)
. Noting that you can want to do "raise a base to the power of my series" for a base other than np.e
, you might want the use case of ser.exp(base=other_ser, level=my_level)
. Don't think the other approaches do that particularly well.
Comment From: mroeschke
Since other maintainers have not weighed in in support in the meantime, going to close