Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Winsorize data in Pandas for Python

I am trying to run a Winsorized regression in pandas for Python. The very helpful user manual offers this example code:

winz = rets.copy()
std_1year = rolling_std(rets, 250, min_periods=20)
cap_level = 3 * np.sign(winz) * std_1year
winz[np.abs(winz) > 3 * std_1year] = cap_level
winz_model = ols(y=winz['AAPL'], x=winz.ix[:, ['GOOG']],window=250)

The fourth line looks wrong to me: shouldn't the RHS be cap_level[np.abs(winz) > 3 * std_1year]?

Thanks for the help! I'm still new to using the Pandas dataframe and want to make sure I'm understanding right.

like image 967
David M Avatar asked Aug 04 '26 18:08

David M


1 Answers

Edit: sorry, misunderstood the question!

You're correct that this would be wrong for most types; however pandas.DataFrame has special support for setting values using a Boolean mask; it will select the corresponding values from the RHS with the corresponding time value. Under the hood it's using np.putmask.

You can check this for yourself:

>>> df = pandas.DataFrame(np.linspace(0.0, 1.0, 10).reshape(5, 2))
>>> df[df > 0.5] = -df
>>> df
          0         1
0  0.000000  0.111111
1  0.222222  0.333333
2  0.444444 -0.555556
3 -0.666667 -0.777778
4 -0.888889 -1.000000
like image 186
ecatmur Avatar answered Aug 07 '26 07:08

ecatmur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!