How to get the final rows of a time series data using pandas series.last() method?


The pandas series.last() method is used to return final periods based on the date offset. By applying this series.last() method we can get the final periods of the time series object. The last method is very similar to the pandas series.first() method, here we can get the final periods instead of the initial periods.

The series.last() method has a parameter called offset which is used to mention the length of the offset data to select the rows within the given limit.

The last() method will return a now Series object with resultant rows and it will raise the TypeError if the index of the input series object doesn’t have the DatetimeIndex.

Example 1

In this following example, a series “s” is created by using the pandas DateTime index with the corresponding month day names to the index.

# importing packages
import pandas as pd

# creating dates
dates = pd.date_range('2021-02-01', periods=10, freq='20D')

#creating pandas Series with date index
s = pd.Series(dates.strftime('%B___%A'), index= dates)
print (s)

# get the rows by using last method
result = s.last('1M')
print('Result:')
print(result)

Explanation

Here, we applied the last() method with offset “1M” to get the final rows within one month.

Output

The output is given below −

2021-02-01    February___Monday
2021-02-21    February___Sunday
2021-03-13    March___Saturday
2021-04-02    April___Friday
2021-04-22    April___Thursday
2021-05-12    May___Wednesday
2021-06-01    June___Tuesday
2021-06-21    June___Monday
2021-07-11    July___Sunday
2021-07-31    July___Saturday
Freq: 20D, dtype: object
Result:
2021-07-11    July___Sunday
2021-07-31    July___Saturday
Freq: 20D, dtype: object

The instances 2021-07-11 and 2021-07-31 are the final month within a one-month duration. We can see both the initial series object, as well as the final series object in the above output block.

Example 2

In the same way, we have created a pandas series object with pandas DateTime index of frequency 2days and 15 hours.

# importing packages
import pandas as pd

# creating dates
dates = pd.date_range('2021-03-10', periods=10, freq='2D15H')

#creating pandas Series with date index
s = pd.Series(dates.strftime('%A_%b_%d'), index= dates)
print (s)

# get the rows by using last method
result = s.last('1W')
print('Result:')
print(result)

Explanation

Here, we are retrieving the rows by specifying offset “1W” one week to the last method.

Output

The output is as follows −

2021-03-10 00:00:00    Wednesday_Mar_10
2021-03-12 15:00:00    Friday_Mar_12
2021-03-15 06:00:00    Monday_Mar_15
2021-03-17 21:00:00    Wednesday_Mar_17
2021-03-20 12:00:00    Saturday_Mar_20
2021-03-23 03:00:00    Tuesday_Mar_23
2021-03-25 18:00:00    Thursday_Mar_25
2021-03-28 09:00:00    Sunday_Mar_28
2021-03-31 00:00:00    Wednesday_Mar_31
2021-04-02 15:00:00    Friday_Apr_02
Freq: 63H, dtype: object

Result:
2021-03-31 00:00:00    Wednesday_Mar_31
2021-04-02 15:00:00    Friday_Apr_02
Freq: 63H, dtype: object

The last method has successfully returned a series object with 2 instances. And these 2 rows have the index within a week.

Updated on: 07-Mar-2022

347 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements