pandas.Series.first#

Series.first(offset)[source] #

Select initial periods of time series data based on a date offset.

Deprecated since version 2.1: first() is deprecated and will be removed in a future version. Please create a mask and filter using .loc instead.

For a DataFrame with a sorted DatetimeIndex, this function can select the first few rows based on a date offset.

Parameters:
offsetstr, DateOffset or dateutil.relativedelta

The offset length of the data that will be selected. For instance, ‘1ME’ will display all the rows having their index within the first month.

Returns:
Series or DataFrame

A subset of the caller.

Raises:
TypeError

If the index is not a DatetimeIndex

See also

last

Select final periods of time series based on a date offset.

at_time

Select values at a particular time of the day.

between_time

Select values between particular times of the day.

Examples

>>> i = pd.date_range('2018年04月09日', periods=4, freq='2D')
>>> ts = pd.DataFrame({'A': [1, 2, 3, 4]}, index=i)
>>> ts
 A
2018年04月09日 1
2018年04月11日 2
2018年04月13日 3
2018年04月15日 4

Get the rows for the first 3 days:

>>> ts.first('3D')
 A
2018年04月09日 1
2018年04月11日 2

Notice the data for 3 first calendar days were returned, not the first 3 days observed in the dataset, and therefore data for 2018年04月13日 was not returned.