FutureWarning: Method .ptp
Asked Answered
P

4

9

I want to know which line or method caused the Future Warning!

predictors = weekly.columns[1:7] # the lags and volume
X = sm.add_constant(weekly[predictors]) # sm: statsmodels
y = np.array([1 if el=='Up' else 0 for el in weekly.Direction.values])
logit = sm.Logit(y,X)
results=logit.fit()
print(results.summary())

C:\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead. return ptp(axis=axis, out=out, **kwargs)

Ptero answered 26/5, 2019 at 5:29 Comment(1)
FYI: pandas.pydata.org/pandas-docs/stable/reference/api/…Mauldon
P
9

weekly[predictors] will return a Series representation of the weekly[[predictors]] DataFrame. Since the warning tells to use numpy.ptp, then by adding the attribute values to weekly[predictors] will make the warning disappeared, i.e.

X = sm.add_constant(weekly[predictors].values)

or you can use the method to_numpy():

X = sm.add_constant(weekly[predictors].to_numpy())

It will convert the weekly[predictors] Series to a NumPy array.

Pavement answered 21/7, 2019 at 6:13 Comment(3)
Unfortunately this method will return a numpy array, not a dataframe, which is annoying because you lose information like the column names and whatnot.Heterosexuality
X = weekly[predictors] X['const'] = 1 These will give headers as well.Unpeople
Thanks @user3476359, easiest solution. If one cares about preserving also the column order: sm.add_constant adds the const as the first column, and X.insert(0,'const',1) does the same.Courcy
I
2

The line which generate this warning is this:

X = sm.add_constant(weekly[predictors]) # sm: statsmodels

Unfortunately i have the same issue.

Immiscible answered 4/7, 2019 at 14:56 Comment(0)
A
1

The line that causes the warning is:

X = sm.add_constant(weekly[predictors]) # sm: statsmodels

This is a utility from statsmodels that adds a column called const to the data frame with all 1.

Since it doesn't work anymore (uses a deprecated function), you can use a different method. I prefer pandas' built-in assign method:

X = weekly[predictors].assign(const=1)

Or even, call it Intercept because that's what that constant is for, and to be consistent with the formula api in statsmodels.

X = weekly[predictors].assign(Intercept=1)

Actin answered 8/12, 2019 at 8:24 Comment(0)
H
0

If you want to preserve a dataframe instead of returning a numpy array:

X = pd.DataFrame(sm.add_constant(weekly[predictors].values, has_constant='add'), columns = ['const'] + weekly[predictors].columns.tolist()) 

Also, your y is already a numpy array, but if y also happens to be a pandas series you might need to call y.reset_index() because once you turn X into a numpy array you lose whatever indices you had.

Heterosexuality answered 23/9, 2019 at 16:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.