Pandas Series: first() function
Subset initial periods of Pandas time series
The first() function (convenience method ) is used to subset initial periods of time series data based on a date offset.
Syntax:
Series.first(self, offset)
Parameters:
Name | Description | Type/Default Value | Required / Optional |
---|---|---|---|
offset | Keep labels from axis which are in items. | 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 first 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.first('3D')
Output:
X 2019-02-09 1 2019-02-11 2
Notice the data for 3 first calender days were returned, not the first 3 days observed in the dataset,and therefore data for 2019-02-13 was not returned.
Previous: Test Pandas objects contain the same elements
Next: Get the first n rows in Pandas series
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://www.w3resource.com/pandas/series/series-first.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics