Pandas Series: last() function
Subsetting final periods of time in Pandas series
The last() function (convenience method ) is used to subset final periods of time series data based on a date offset.
Return a boolean Series showing whether each element in the Series matches an element in the passed sequence of values exactly.
Syntax:
Series.last(self, offset)[source]
Parameters:
Name | Description | Type/Default Value | Required / Optional |
---|---|---|---|
offset | string, DateOffset Default Value: relativedelta |
Required |
Returns: subset - same type as caller
Raises: TypeError
If the index is not a DatetimeIndex
Example:
Python-Pandas Code:
import numpy as np
import pandas as pd
i = pd.date_range('2019-02-09', periods=4, freq='2D')
ts = pd.DataFrame({'X': [1,2,3,4]}, index=i)
ts
Output:
X 2019-02-09 1 2019-02-11 2 2019-02-13 3 2019-02-15 4
Example - Get the rows for the last 3 days:
Python-Pandas Code:
import numpy as np
import pandas as pd
i = pd.date_range('2019-02-09', periods=4, freq='2D')
ts = pd.DataFrame({'X': [1,2,3,4]}, index=i)
ts.last('3D')
Output:
X 2019-02-13 3 2019-02-15 4
Note: The data for 3 last calender days were returned, not the last 3 observed days in the dataset, and therefore data for 2019-02-11 was not returned.
Previous: Find values contained in Pandas series
Next: Conform series in Pandas
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/pandas/series/series-last.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics