Skip to content Skip to sidebar Skip to footer

Why Can't I Apply Shift From Within A Pandas Function?

I am trying to build a function that uses .shift() but it is giving me an error. Consider this: In [40]: data={'level1':[20,19,20,21,25,29,30,31,30,29,31], 'level2': [10,10,

Solution 1:

Try passing the frame to the function, rather than using apply (I am not sure why apply doesn't work, even column-wise):

deff(x):
    x.level1 
    return x.level1 + x.level1.shift(1)

f(frame)

returns:

2014-12-01   NaN2014-12-02    392014-12-03    392014-12-04    412014-12-05    462014-12-06    542014-12-07    592014-12-08    612014-12-09    612014-12-10    592014-12-11    60Freq:D,Name:level1,dtype:float64

Solution 2:

Check if the values you are trying to shift is not an array. Then you need to convert the array to series. With this you will be able to shift the values. I was having same issues,now I am able to get the shift values.

This is my part of the code for your reference.

X = grouped['Confirmed_day'].values
X_series=pd.Series(X)

X_lag1 = X_series.shift(1)

Solution 3:

I'm not entirely following along, but if frame['level1'].shift(1) works, then I can only imagine that frame['level1'] is not a numpy.int64 object while whatever you are passing into the verticaladd function is. Probably need to look at your types.

Post a Comment for "Why Can't I Apply Shift From Within A Pandas Function?"