Signup/Sign In

Pandas DataFrame first() Method

In this tutorial, we will learn the Python pandas DataFrame.first() method. It selects the initial periods of time series data based on a date offset. When having a DataFrame with dates as the index, this method can select the first few rows based on a date offset. It returns the DataFrame and raises TypeError if the index is not a DatetimeIndex.

The below shows the syntax of the DataFrame.first() method.

Syntax

DataFrame.first(offset)

Parameters

offset: str, DateOffset, or dateutil.relativedelta. The offset length of the data that will be selected. For instance, ‘1M’ will display all the rows having their index within the first month.

Example: Getting the rows bu using the DataFrame.first() Method

The below example shows getting the rows for the first 3 days. In the below example, the data for the 3 first calendar days were returned, not the first 3 days observed in the dataset, and therefore data for 2021-01-13 was not returned.

#importing pandas as pd
import pandas as pd
i = pd.date_range('2021-01-09', periods=4, freq='2D')
df = pd.DataFrame({'A': [1, 2, 3, 4]}, index=i)
print("The DataFrame is")
print(df)
print(df.first('3D'))

Once we run the program we will get the following output.


The DataFrame is
A
2021-01-09 1
2021-01-11 2
2021-01-13 3
2021-01-15 4
A
2021-01-09 1
2021-01-11 2

Example: Getting the rows by using the DataFrame.first() Method

The below example is similar to the previous one except it returns the first two days.

#importing pandas as pd
import pandas as pd
i = pd.date_range('2021-01-09', periods=4, freq='4D')
df = pd.DataFrame({'A': [1, 2, 3, 4]}, index=i)
print("The DataFrame is")
print(df)
print(df.first('2D'))

Once we run the program we will get the following output.


The DataFrame is
A
2021-01-09 1
2021-01-13 2
2021-01-17 3
2021-01-21 4
A
2021-01-09 1

Example: Getting the rows for the first 'n' months using the DataFrame.first() Method

The below example shows getting the rows for the first 1 month.

#importing pandas as pd
import pandas as pd
i = pd.date_range('2021-01-01', periods=4, freq='1M')
df = pd.DataFrame({'A': [1, 2, 3, 4]}, index=i)
print("The DataFrame is")
print(df)
print(df.first('1M'))

Once we run the program we will get the following output.


The DataFrame is
A
2021-01-31 1
2021-02-28 2
2021-03-31 3
2021-04-30 4
A
2021-01-31 1
2021-02-28 2

Example: TypeError in the DataFrame.first() Method

The DataFrame.first() method raises TypeError if the index is not a DatetimeIndex.

#importing pandas as pd
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3, 4]})
print(df.first('1M'))

Once we run the program we will get the following output.


TypeError: 'first' only supports a DatetimeIndex index

Conclusion

In this tutorial, we learned the Python pandas DataFrame.first() method. We learned the syntax and by applying this method on the DataFrame we solved examples and understood the DataFrame.first() method.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.