pct_change calculation should have an additional optional parameter for calculation with negative numbers
See original GitHub issueProblem description
I’m proposing addition of an additional optional parameter for pct_change calculation to help with negative numbers calculations. The new optional calculation should be calculated as s.diff()/s.shift().abs()
.
Example code of current and proposed behavior
s = pd.Series([-1,-2,-1, 1])
res = pd.concat((s, s.pct_change(), s.diff()/s.shift().abs()),1)
res.columns = ['series','current pct_change','proposed alternative']
res
series current pct_change proposed alternative
0 -1 NaN NaN
1 -2 1.0 -1.0
2 -1 -0.5 0.5
3 1 -2.0 2.0
Intuition and Expected Behavior
In finance, when going from -1 to -2 one is going from less debt to more debt , hence one would expect negative -100% change not positive +100% change. Say you had -1$ debt, and now you have -2$ debt, your debt increased by -100%. In absolute terms, the value changed by 100%, but the direction is negative not positive. Similarly, when going reducing debt from -2 to -1, pandas produces -50% instead of +50%.
A more complex example is going from -1 (negative) to +1 (positive), one would think of that as a positive change, that is a person went from negative balance to positive balance. However, pandas produces negative -200% instead of +200%.
The issue is that there is no universal approach for calculating pct_change when there are negative numbers in the series. For that reason, pandas should provide an optional parameter for dealing with negative numbers.
To give you more details of the issue, why pandas pct_change calculation produces nonsensical values when negative numbers are in the series, you can take a look at this article:
(https://www.excelcampus.com/functions/percentage-change-formula-negative-numbers/)
Output of pd.show_versions()
pandas: 0.23.4
Original post updated to incorporate below comments.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:6
- Comments:6 (3 by maintainers)
Top GitHub Comments
Agreed on the first point but not on the second. I don’t see a keyword providing much value here over a user using
abs
as desired to get the calculation they want. Closing as a resultprobably a bit late to the game 😃 But something like :
import numpy as np
df[column].pct_change()*np.sign(df[column].shift(periods=1))
should do the job