Skip to content

DataFrame.shift unexpected result #11195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
121onto opened this issue Sep 26, 2015 · 3 comments
Closed

DataFrame.shift unexpected result #11195

121onto opened this issue Sep 26, 2015 · 3 comments
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@121onto
Copy link

121onto commented Sep 26, 2015

Consider an arbitrary DataFrame:

df = pd.DataFrame([1,2,4,35,356,3425,24,24,12,24,6,7,8,8,4], columns=['one'])
df['two'] = np.log(df['one'])
df['three'] = np.log10(df['one'])
df['four'] = np.exp(df['one'])
df['five'] = df['two'] * df['three']

This gives:

In [8]: df
Out[8]:
     one       two     three           four       five
0      1  0.000000  0.000000   2.718282e+00   0.000000
1      2  0.693147  0.301030   7.389056e+00   0.208658
2      4  1.386294  0.602060   5.459815e+01   0.834632
3     35  3.555348  1.544068   1.586013e+15   5.489699
4    356  5.874931  2.551450  4.062895e+154  14.989592
5   3425  8.138857  3.534661            inf  28.768096
6     24  3.178054  1.380211   2.648912e+10   4.386386
7     24  3.178054  1.380211   2.648912e+10   4.386386
8     12  2.484907  1.079181   1.627548e+05   2.681665
9     24  3.178054  1.380211   2.648912e+10   4.386386
10     6  1.791759  0.778151   4.034288e+02   1.394260
11     7  1.945910  0.845098   1.096633e+03   1.644485
12     8  2.079442  0.903090   2.980958e+03   1.877923
13     8  2.079442  0.903090   2.980958e+03   1.877923
14     4  1.386294  0.602060   5.459815e+01   0.834632

Now shift columns:

In [9]: df.shift(axis=1)
Out[9]:
    one  two  three  four  five
0   NaN  NaN    NaN   NaN   NaN
1   NaN  NaN    NaN   NaN   NaN
2   NaN  NaN    NaN   NaN   NaN
3   NaN  NaN    NaN   NaN   NaN
4   NaN  NaN    NaN   NaN   NaN
5   NaN  NaN    NaN   NaN   NaN
6   NaN  NaN    NaN   NaN   NaN
7   NaN  NaN    NaN   NaN   NaN
8   NaN  NaN    NaN   NaN   NaN
9   NaN  NaN    NaN   NaN   NaN
10  NaN  NaN    NaN   NaN   NaN
11  NaN  NaN    NaN   NaN   NaN
12  NaN  NaN    NaN   NaN   NaN
13  NaN  NaN    NaN   NaN   NaN
14  NaN  NaN    NaN   NaN   NaN

It appears this issue has been around for over a year:

http://stackoverflow.com/questions/23105197/shift-entire-column-on-a-pandas-dataframe

@chris-b1
Copy link
Contributor

In 0.17/master here's what happens - closer but it looks like the int/float blocks are shifted separately.

In [3]: df.shift(axis=1)
Out[3]: 
    one  two     three      four           five
0   NaN  NaN  0.000000  0.000000   2.718282e+00
1   NaN  NaN  0.693147  0.301030   7.389056e+00
2   NaN  NaN  1.386294  0.602060   5.459815e+01
3   NaN  NaN  3.555348  1.544068   1.586013e+15
4   NaN  NaN  5.874931  2.551450  4.062895e+154
5   NaN  NaN  8.138857  3.534661            inf
6   NaN  NaN  3.178054  1.380211   2.648912e+10
7   NaN  NaN  3.178054  1.380211   2.648912e+10
8   NaN  NaN  2.484907  1.079181   1.627548e+05
9   NaN  NaN  3.178054  1.380211   2.648912e+10
10  NaN  NaN  1.791759  0.778151   4.034288e+02
11  NaN  NaN  1.945910  0.845098   1.096633e+03
12  NaN  NaN  2.079442  0.903090   2.980958e+03
13  NaN  NaN  2.079442  0.903090   2.980958e+03
14  NaN  NaN  1.386294  0.602060   5.459815e+01

A workaround that works in both 0.16 and master to cast your frame to floats first.

In [8]: pd.__version__
Out[8]: '0.16.2'

In [9]: df.astype('float').shift(axis=1)
Out[9]: 
    one   two     three      four           five
0   NaN     1  0.000000  0.000000   2.718282e+00
1   NaN     2  0.693147  0.301030   7.389056e+00
2   NaN     4  1.386294  0.602060   5.459815e+01
3   NaN    35  3.555348  1.544068   1.586013e+15
4   NaN   356  5.874931  2.551450  4.062895e+154
5   NaN  3425  8.138857  3.534661            inf
6   NaN    24  3.178054  1.380211   2.648912e+10
7   NaN    24  3.178054  1.380211   2.648912e+10
8   NaN    12  2.484907  1.079181   1.627548e+05
9   NaN    24  3.178054  1.380211   2.648912e+10
10  NaN     6  1.791759  0.778151   4.034288e+02
11  NaN     7  1.945910  0.845098   1.096633e+03
12  NaN     8  2.079442  0.903090   2.980958e+03
13  NaN     8  2.079442  0.903090   2.980958e+03
14  NaN     4  1.386294  0.602060   5.459815e+01

@jreback
Copy link
Contributor

jreback commented Sep 26, 2015

this is already covered by #10539

@jreback jreback closed this as completed Sep 26, 2015
@jreback jreback added Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Sep 26, 2015
@jreback
Copy link
Contributor

jreback commented Sep 26, 2015

@121onto pull requests are always welcome to fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

No branches or pull requests

3 participants