pandas shift rows NaNs
It's possible this is a bug, you can use np.roll
to achieve this:
In[11]:x.apply(lambda x: np.roll(x, 2), axis=1)Out[11]: col0 col1 col2 col3 col4 col5 col6 col70 NaN NaN 6.0 5.0 1.0 5.0 2.0 4.01 NaN NaN 8.0 8.0 9.0 6.0 7.0 2.02 NaN NaN 8.0 3.0 9.0 6.0 6.0 6.03 NaN NaN 8.0 4.0 4.0 4.0 8.0 9.04 NaN NaN 5.0 3.0 4.0 3.0 8.0 7.0
Speedwise, it's probably quicker to construct a df and reuse the existing columns and pass the result of np.roll
as the data arg to the constructor to DataFrame
:
In[12]:x = pd.DataFrame(np.roll(x, 2, axis=1), columns = x.columns)xOut[12]: col0 col1 col2 col3 col4 col5 col6 col70 NaN NaN 6.0 5.0 1.0 5.0 2.0 4.01 NaN NaN 8.0 8.0 9.0 6.0 7.0 2.02 NaN NaN 8.0 3.0 9.0 6.0 6.0 6.03 NaN NaN 8.0 4.0 4.0 4.0 8.0 9.04 NaN NaN 5.0 3.0 4.0 3.0 8.0 7.0
timings
In[13]:%timeit pd.DataFrame(np.roll(x, 2, axis=1), columns = x.columns)%timeit x.fillna(0).astype(int).shift(2, axis=1)10000 loops, best of 3: 117 µs per loop1000 loops, best of 3: 418 µs per loop
So constructing a new df with the result of np.roll
is quicker than first filling the NaN
values, cast to int
, and then shift
ing.