Python shift – Python Pandas Series shift() Function

Pandas Series shift() Function:

Python shift: The shift() function of the Pandas Series shifts the index by the given number of periods, with an optional time freq.

When no frequency is specified, the function shifts the index without realigning the data. If freq is specified (the index must be date or datetime else a NotImplementedError will be raised), the index will be incremented using the periods and the freq.

Syntax:

Series.shift(periods=1, freq=None, axis=0, fill_value)

Parameters

periods: This is optional. It represents the number of periods to shift. It can be both positive and negative. 1 is the default.

freq: This is optional. It indicates freqDateOffset, tseries.offsets, timedelta, or str. It is the offset to use from the tseries module or time rule (for example, ‘EOM’). The index values are shifted but the data is not realigned if freq is given. None is the default.

axis: This is optional. It indicates 0 or ‘index’, 1 or ‘columns’. If the value is 0 or ‘index,’ the shift is in the column direction. If 1 or ‘columns’ is specified, the shift occurs in the row direction. 0 is the default.

fill_value: This is optional. It is the scalar value that will be used for newly added missing values. The default value is self.dtype.na_value.

Return Value:

The shifted input object is returned by the shift() function of the Pandas Series.

Pandas Series shift() Function in Python

Example1

Approach:

  • Import pandas module using the import keyword.
  • Pass some random key-value pair(dictionary), index list(start, end dates) as arguments to the DataFrame() function of the pandas module to create a dataframe
  • Store it in a variable.
  • Print the given dataframe
  • Apply shift() function the given dataframe to shift the dataframe by 1 period column-wise and print the result
  • Apply shift() function the given dataframe by passing some random number k as an argument to shift the dataframe by k periods column-wise and print the result.
  • Apply shift() function the given dataframe by passing axis=1 as an argument to shift the dataframe by 1 period row-wise and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random key-value pair(dictionary), index list(start, end dates)
# as arguments to the DataFrame() function of the pandas module 
# to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "first_column": [10, 15, 25, 35, 45],
  "second_column": [1, 2, 3, 4, 5],
  "third_column": [3, 5, 7, 9, 11]},
  index=pd.date_range("2019-06-01", "2019-06-05")
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()
# Apply shift() function the given dataframe to shift the dataframe
# by 1 period column-wise and print the result
print("Shifting the dataframe by 1 period column-wise:")
print(data_frme.shift())
print()
# Apply shift() function the given dataframe by passing some random number k 
# as an argument to shift the dataframe by k periods column-wise and print the result.
print("Shifting the dataframe by 2 periods column-wise:")
print(data_frme.shift(2))
print()
# Apply shift() function the given dataframe by passing axis=1 as  
# an argument to shift the dataframe by 1 period row-wise and print the result.
print("Shifting the given dataframe by 1 period row-wise:")
print(data_frme.shift(axis=1))

Output:

The given Dataframe:
            first_column  second_column  third_column
2019-06-01            10              1             3
2019-06-02            15              2             5
2019-06-03            25              3             7
2019-06-04            35              4             9
2019-06-05            45              5            11

Shifting the dataframe by 1 period column-wise:
            first_column  second_column  third_column
2019-06-01           NaN            NaN           NaN
2019-06-02          10.0            1.0           3.0
2019-06-03          15.0            2.0           5.0
2019-06-04          25.0            3.0           7.0
2019-06-05          35.0            4.0           9.0

Shifting the dataframe by 2 periods column-wise:
            first_column  second_column  third_column
2019-06-01           NaN            NaN           NaN
2019-06-02           NaN            NaN           NaN
2019-06-03          10.0            1.0           3.0
2019-06-04          15.0            2.0           5.0
2019-06-05          25.0            3.0           7.0

Shifting the given dataframe by 1 period row-wise:
            first_column  second_column  third_column
2019-06-01           NaN             10             1
2019-06-02           NaN             15             2
2019-06-03           NaN             25             3
2019-06-04           NaN             35             4
2019-06-05           NaN             45             5

Example2

Approach:

  • Import pandas module using the import keyword.
  • Pass some random key-value pair(dictionary), index list(start, end dates) as arguments to the DataFrame() function of the pandas module to create a dataframe
  • Store it in a variable.
  • Print the given dataframe.
  • Apply shift() function the given dataframe by passing some random number k as an argument to shift the dataframe by k periods column-wise and print the result.
  • Apply shift() function the given dataframe by passing some random number k, fill_value as the arguments to shift the dataframe by k periods column-wise with the given fill_value and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random key-value pair(dictionary), index list(start, end dates)
# as arguments to the DataFrame() function of the pandas module 
# to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "first_column": [10, 15, 25, 35, 45],
  "second_column": [1, 2, 3, 4, 5],
  "third_column": [3, 5, 7, 9, 11]},
  index=pd.date_range("2019-06-01", "2019-06-05")
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()

# Apply shift() function the given dataframe by passing some random number k 
# as an argument to shift the dataframe by k periods column-wise and print the result.
print("Shifting the dataframe by 2 periods column-wise:")
print(data_frme.shift(2))
print()

# Apply shift() function the given dataframe by passing some random number k, fill_value 
# as the arguments to shift the dataframe by k periods column-wise with the given fill_value 
# and print the result.
print("Shifting the dataframe by 2 periods column-wise with fill_value=100:")
print(data_frme.shift(2, fill_value=100))

Output:

The given Dataframe:
            first_column  second_column  third_column
2019-06-01            10              1             3
2019-06-02            15              2             5
2019-06-03            25              3             7
2019-06-04            35              4             9
2019-06-05            45              5            11

Shifting the dataframe by 2 periods column-wise:
            first_column  second_column  third_column
2019-06-01           NaN            NaN           NaN
2019-06-02           NaN            NaN           NaN
2019-06-03          10.0            1.0           3.0
2019-06-04          15.0            2.0           5.0
2019-06-05          25.0            3.0           7.0

Shifting the dataframe by 2 periods column-wise with fill_value=100:
            first_column  second_column  third_column
2019-06-01           100            100           100
2019-06-02           100            100           100
2019-06-03            10              1             3
2019-06-04            15              2             5
2019-06-05            25              3             7

Example3

Approach:

  • Import pandas module using the import keyword.
  • Pass some random key-value pair(dictionary), index list(start, end dates) as arguments to the DataFrame() function of the pandas module to create a dataframe
  • Store it in a variable.
  • Print the given dataframe.
  • Apply shift() function the given dataframe by passing periods as some random number k, freq=’D’ as the arguments to shift the index by k periods with frequency D(date) and print the result.
  • Apply shift() function the given dataframe by passing periods as some random number k, freq=’infer’ as the arguments to shift the index by k periods with frequency infer and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random key-value pair(dictionary), index list(start, end dates)
# as arguments to the DataFrame() function of the pandas module 
# to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "first_column": [10, 15, 25, 35, 45],
  "second_column": [1, 2, 3, 4, 5],
  "third_column": [3, 5, 7, 9, 11]},
  index=pd.date_range("2019-06-01", "2019-06-05")
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()

# Apply shift() function the given dataframe by passing periods as some random number k,
# freq='D' as the arguments to shift the index by k periods with frequency D(date)
# and print the result.
print("Shifting the index by 2 periods with freq=D:")
print(data_frme.shift(periods=2, freq='D'))
# Apply shift() function the given dataframe by passing periods as some random number k,
# freq='infer' as the arguments to shift the index by k periods with frequency infer
# and print the result.
print("Shifting the index by 2 periods with freq=infer:")
print(data_frme.shift(periods=2, freq='infer'))

Output:

The given Dataframe:
            first_column  second_column  third_column
2019-06-01            10              1             3
2019-06-02            15              2             5
2019-06-03            25              3             7
2019-06-04            35              4             9
2019-06-05            45              5            11

Shifting the index by 2 periods with freq=D:
            first_column  second_column  third_column
2019-06-03            10              1             3
2019-06-04            15              2             5
2019-06-05            25              3             7
2019-06-06            35              4             9
2019-06-07            45              5            11
Shifting the index by 2 periods with freq=infer:
            first_column  second_column  third_column
2019-06-03            10              1             3
2019-06-04            15              2             5
2019-06-05            25              3             7
2019-06-06            35              4             9
2019-06-07            45              5            11