Last updated: Apr 12, 2024
Reading timeยท4 min
To get the max and min dates in a Pandas DataFrame:
max()
function to get the max date.min()
function to get the min date.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
We used bracket notation to select the date
column.
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.
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.
print(min(50, 100)) # ๐๏ธ 50
Conversely, the max()
function returns the largest item in an iterable or the
largest of two or more arguments.
print(max(50, 100)) # ๐๏ธ 100
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.
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
The DataFrame.index attribute returns
the index of the DataFrame
.
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.
agg()
methodYou can also use the
DataFrame.agg
method to get the max and min dates in a DataFrame
.
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
Running the code sample produces the following output.
min 2021-01-24 max 2023-03-25 Name: date, dtype: object -------------------------------------------------- 2021-01-24 2023-03-25
The DataFrame.agg() method aggregates using one or more operations over the specified axis.
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.
# 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
max()
and min()
methods on the Date columnYou can also call the max()
and min()
methods directly on the date
column.
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
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
.
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
I've also written an article on how to convert Epoch to Datetime in a Pandas DataFrame.
You can learn more about the related topics by checking out the following tutorials: