SoFunction
Updated on 2024-11-07

Introduction to the shift and diff function relationship in pandas

The ? command to view the help file

Signature: (self, periods=1, freq=None, axis=0) 
Docstring: 
Shift index by desired number of periods with an optional time freq 

The main function of this function is to make the data in the data frame to move, if freq=None, according to the setting of axis, the row index data remain unchanged, the column index data can be moved up and down on the rows or left and right on the columns; if the row index for the time series, you can set the freq parameter, according to the combination of periods and freq parameter values, so that the row index each time the occurrence of the periods*freq offset roll, the column index data will not move

① For DataFrame, the row index is date type, the row index is moved, and the column index data remains unchanged.

In [2]: import pandas as pd
  ...: import numpy as np
  ...: df = ((24).reshape(6,4),index=pd.date_range(start=
  ...: '20170101',periods=6),columns=['A','B','C','D'])
  ...: df
  ...:
Out[2]:
       A  B  C  D
2017-01-01  0  1  2  3
2017-01-02  4  5  6  7
2017-01-03  8  9 10 11
2017-01-04 12 13 14 15
2017-01-05 16 17 18 19
2017-01-06 20 21 22 23
In [3]: (2,axis=0,freq='2D')
Out[3]:
       A  B  C  D
2017-01-05  0  1  2  3
2017-01-06  4  5  6  7
2017-01-07  8  9 10 11
2017-01-08 12 13 14 15
2017-01-09 16 17 18 19
2017-01-10 20 21 22 23
In [4]: (2,axis=1,freq='2D')
Out[4]:
       A  B  C  D
2017-01-05  0  1  2  3
2017-01-06  4  5  6  7
2017-01-07  8  9 10 11
2017-01-08 12 13 14 15
2017-01-09 16 17 18 19
2017-01-10 20 21 22 23
In [5]: (2,freq='2D')
Out[5]:
       A  B  C  D
2017-01-05  0  1  2  3
2017-01-06  4  5  6  7
2017-01-07  8  9 10 11
2017-01-08 12 13 14 15
2017-01-09 16 17 18 19
2017-01-10 20 21 22 23

Conclusion:For time indexes, shift moves the time index, leaving the rest of the data as it is, and axis settings have no effect

② For DataFrame row indexes that are not time series, the row index data remains unchanged and the column index data moves.

In [6]: import pandas as pd
  ...: import numpy as np
  ...: df = ((24).reshape(6,4),index=['r1','r2','r3','r4'
  ...: ,'r5','r6'],columns=['A','B','C','D'])
  ...: df
  ...:
Out[6]:
   A  B  C  D
r1  0  1  2  3
r2  4  5  6  7
r3  8  9 10 11
r4 12 13 14 15
r5 16 17 18 19
r6 20 21 22 23
In [7]: (periods=2,axis=0)
Out[7]:
    A   B   C   D
r1  NaN  NaN  NaN  NaN
r2  NaN  NaN  NaN  NaN
r3  0.0  1.0  2.0  3.0
r4  4.0  5.0  6.0  7.0
r5  8.0  9.0 10.0 11.0
r6 12.0 13.0 14.0 15.0
In [8]: (periods=-2,axis=0)
Out[8]:
    A   B   C   D
r1  8.0  9.0 10.0 11.0
r2 12.0 13.0 14.0 15.0
r3 16.0 17.0 18.0 19.0
r4 20.0 21.0 22.0 23.0
r5  NaN  NaN  NaN  NaN
r6  NaN  NaN  NaN  NaN
In [9]: (periods=2,axis=1)
Out[9]:
   A  B   C   D
r1 NaN NaN  0.0  1.0
r2 NaN NaN  4.0  5.0
r3 NaN NaN  8.0  9.0
r4 NaN NaN 12.0 13.0
r5 NaN NaN 16.0 17.0
r6 NaN NaN 20.0 21.0
In [10]: (periods=-2,axis=1)
Out[10]:
    A   B  C  D
r1  2.0  3.0 NaN NaN
r2  6.0  7.0 NaN NaN
r3 10.0 11.0 NaN NaN
r4 14.0 15.0 NaN NaN
r5 18.0 19.0 NaN NaN
r6 22.0 23.0 NaN NaN

The ? command, I checked the help file and found that it is the same form as the shift function

Signature: (self, periods=1, axis=0) 
Docstring: 
1st discrete difference of object 

Here's a look at the relationship between the diff function and the shift function

In [13]: (periods=2,axis=0)
Out[13]:
   A  B  C  D
r1 NaN NaN NaN NaN
r2 NaN NaN NaN NaN
r3 8.0 8.0 8.0 8.0
r4 8.0 8.0 8.0 8.0
r5 8.0 8.0 8.0 8.0
r6 8.0 8.0 8.0 8.0
In [14]: df -(periods=2,axis=0)
Out[14]:
    A   B   C   D
r1  NaN  NaN  NaN  NaN
r2  NaN  NaN  NaN  NaN
r3  0.0  1.0  2.0  3.0
r4  4.0  5.0  6.0  7.0
r5  8.0  9.0 10.0 11.0
r6 12.0 13.0 14.0 15.0
In [15]: (periods=2,axis=0)
Out[15]:
    A   B   C   D
r1  NaN  NaN  NaN  NaN
r2  NaN  NaN  NaN  NaN
r3  0.0  1.0  2.0  3.0
r4  4.0  5.0  6.0  7.0
r5  8.0  9.0 10.0 11.0
r6 12.0 13.0 14.0 15.0

Above this talk pandas in shift and diff function relationship is all I share with you, I hope to give you a reference, and I hope you support me more.