The Pandas tseries ofsets MonthEnd function rollforward() used to roll specified date forward to next offset only if not on offset. It returns a rolled timestamp if not on offset, otherwise unchanged timestamp.
1 import pandas as pd
2
3
4 tstamp = pd.Timestamp(2022, 1, 25)
5 offset = pd.offsets.MonthEnd().rollforward(tstamp)
6 print(f'The roll forwarded timestamp : {offset}')
In the above example, a timestamp is created that is not a last day of the month. A rollforward() function is called by passing a timestamp that returns the end of the current month. The result is assign to the variable that will be printed on console.
1 The roll forwarded timestamp : 2022-01-31 00:00:00
rollforward() with unchanged timestamp
rollforward() with unchanged timestamp
1 import pandas as pd
2
3
4 tstamp = pd.Timestamp(2022, 1, 31)
5 offset = pd.offsets.MonthEnd().rollforward(tstamp)
6 print(f'The roll forwarded timestamp : {offset}')
In the above example, a timestamp is created that is a last day of the month. A rollforward() function is called by passing a timestamp that returns unchanged timestamp.
1 The roll forwarded timestamp : 2022-01-31 00:00:00
Example 2
1 import pandas as pd
2
3 tstamp = pd.Timestamp(2022, 1, 1)
4 offset = pd.offsets.MonthEnd().rollforward(tstamp)
5
6 print(f'The month timestamp : {tstamp}')
7 print(f'The roll forwarded timestamp : {offset}')
In the above example, a timestamp is created that is a first day of the month. A rollforward() function is called by passing a timestamp that returns the end of the current month.
1 The month timestamp : 2022-01-01 00:00:00
2 The roll forwarded timestamp : 2022-01-31 00:00:00
Example 3
1 import pandas as pd
2
3
4 tstamp1 = pd.Timestamp(2022, 3, 31)
5 offset1 = pd.offsets.MonthEnd().rollforward(tstamp1)
6
7 print(f'The month timestamp : {tstamp1}')
8 print(f'The roll forwarded timestamp : {offset1}')
In the above example, a timestamp is created that is a last day of the month. A rollforward() function is called by passing a timestamp that returns unchanged timestamp.
1 The month timestamp : 2022-03-31 00:00:00
2 The roll forwarded timestamp : 2022-03-31 00:00:00
Related options for your search