Pandas: How to get the Max and Min Dates in a DataFrame

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
4 min

banner

# Table of Contents

  1. Pandas: How to get the Max and Min Dates in a DataFrame
  2. Getting the Max and Min dates from the Index column of a Pandas DataFrame
  3. Getting the Max and Min dates in a DataFrame using the agg() method
  4. Calling the max() and min() methods on the Date column

# Pandas: How to get the Max and Min Dates in a DataFrame

To get the max and min dates in a Pandas DataFrame:

  1. Use bracket notation to select the date column.
  2. Pass the result to the max() function to get the max date.
  3. Pass the result to the min() function to get the min date.
main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.4], 'date': ['2023-01-05', '2023-03-25', '2021-01-24', '2022-01-29'] }) max_date = max(df['date']) print(max_date) # ๐Ÿ‘‰๏ธ 2023-03-25 min_date = min(df['date']) print(min_date) # ๐Ÿ‘‰๏ธ 2021-01-24

get max and min dates in pandas dataframe

The code for this article is available on GitHub

We used bracket notation to select the date column.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.4], 'date': ['2023-01-05', '2023-03-25', '2021-01-24', '2022-01-29'] }) # 0 2023-01-05 # 1 2023-03-25 # 2 2021-01-24 # 3 2022-01-29 # Name: date, dtype: object print(df['date'])

The last step is to pass the column to the max() function if you need to get the max date or the min() function if you need to get the min date.

main.py
max_date = max(df['date']) print(max_date) # ๐Ÿ‘‰๏ธ 2023-03-25 min_date = min(df['date']) print(min_date) # ๐Ÿ‘‰๏ธ 2021-01-24

The min() function returns the smallest item in an iterable or the smallest of two or more arguments.

main.py
print(min(50, 100)) # ๐Ÿ‘‰๏ธ 50

Conversely, the max() function returns the largest item in an iterable or the largest of two or more arguments.

main.py
print(max(50, 100)) # ๐Ÿ‘‰๏ธ 100

# Getting the Max and Min dates from the Index column of a Pandas DataFrame

If you need to get the max and min dates from the index column of a DataFrame, use the df.index.max() and df.index.min() methods.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.4], }, index=['2023-01-05', '2023-03-25', '2021-01-24', '2022-01-29']) max_date = df.index.max() print(max_date) # ๐Ÿ‘‰๏ธ 2023-03-25 min_date = df.index.min() print(min_date) # ๐Ÿ‘‰๏ธ 2021-01-24

get max and min dates from index column of dataframe

The code for this article is available on GitHub

The DataFrame.index attribute returns the index of the DataFrame.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.4], }, index=['2023-01-05', '2023-03-25', '2021-01-24', '2022-01-29']) # ๐Ÿ‘‡๏ธ Index(['2023-01-05', '2023-03-25', '2021-01-24', '2022-01-29'], dtype='object') print(df.index)

The Index object has max and min attributes that we can use to get the max and min date.

# Getting the Max and Min dates in a DataFrame using the agg() method

You can also use the DataFrame.agg method to get the max and min dates in a DataFrame.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.4], 'date': ['2023-01-05', '2023-03-25', '2021-01-24', '2022-01-29'] }) series = df['date'].agg(['min', 'max']) # min 2021-01-24 # max 2023-03-25 # Name: date, dtype: object print(series) print('-' * 50) print(series[0]) # ๐Ÿ‘‰๏ธ 2021-01-24 print(series[1]) # ๐Ÿ‘‰๏ธ 2023-03-25
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
min 2021-01-24 max 2023-03-25 Name: date, dtype: object -------------------------------------------------- 2021-01-24 2023-03-25

get max and min dates in dataframe using agg method

The DataFrame.agg() method aggregates using one or more operations over the specified axis.

main.py
series = df['date'].agg(['min', 'max'])

The method takes a function, string, list or dict as its first argument.

We passed a list of function names to the method ("min" and "max") to have the min and max functions applied to the date column.

You can use the 0 and 1 indices to get the min and max dates from the series object.

main.py
# min 2021-01-24 # max 2023-03-25 # Name: date, dtype: object print(series) print('-' * 50) print(series[0]) # ๐Ÿ‘‰๏ธ 2021-01-24 print(series[1]) # ๐Ÿ‘‰๏ธ 2023-03-25

# Calling the max() and min() methods on the Date column

You can also call the max() and min() methods directly on the date column.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.4], 'date': ['2023-01-05', '2023-03-25', '2021-01-24', '2022-01-29'] }) print(df['date'].max()) # ๐Ÿ‘‰๏ธ 2023-03-25 print(df['date'].min()) # ๐Ÿ‘‰๏ธ 2021-01-24

call max and min methods on date column

The code for this article is available on GitHub

We first selected the "date" column using bracket notation and then used the max() method to get the max date and the min() method to get the min date.

If you run into issues when calling the methods, try to convert your date column to datetime.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.4], 'date': ['2023-01-05', '2023-03-25', '2021-01-24', '2022-01-29'] }) df['date'] = pd.to_datetime(df['date']) print(df['date'].max()) # ๐Ÿ‘‰๏ธ 2023-03-25 print(df['date'].min()) # ๐Ÿ‘‰๏ธ 2021-01-24

convert column to datetime before calling max min

The code for this article is available on GitHub

I've also written an article on how to convert Epoch to Datetime in a Pandas DataFrame.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev