To filter a pandas DataFrame by date, you can use both basic comparisons with strings representing the date you want to filter by.

import pandas as pd

df = pd.DataFrame({
    "date": ["2021-09-30", "2021-12-31", "2022-03-31", "2022-06-30", "2022-09-30", "2022-12-31"],
    "sales": [100,30,50,60,10,80]
})

df["date"] = pd.to_datetime(df["date"])

df_2022 = df[df["date"] > "2021-12-31"]

print(df_2022)

#Output:
        date  sales
2 2022-03-31     50
3 2022-06-30     60
4 2022-09-30     10
5 2022-12-31     80

You can also use the pandas DataFrame query() function to remove rows by date from a pandas DataFrame.

import pandas as pd

df = pd.DataFrame({
    "date": ["2021-09-30", "2021-12-31", "2022-03-31", "2022-06-30", "2022-09-30", "2022-12-31"],
    "sales": [100,30,50,60,10,80]
})

df["date"] = pd.to_datetime(df["date"])

df_2022 = df.query("date > 20211231")

print(df_2022)

#Output:
        date  sales
2 2022-03-31     50
3 2022-06-30     60
4 2022-09-30     10
5 2022-12-31     80

When working with different collections of data, the ability to easily be able to filter by different conditions is valuable.

One such case is if you want to filter a pandas DataFrame by a date column.

To get rid of rows in a pandas DataFrame by a date column, you can use basic filtering and you can use basic comparisons with strings representing the date you want to filter by.

The most common way to represent a date is with the format “YYYY-MM-DD”. So, if you wanted to only keep rows where the date is greater than December 31st, 2021, you would use “2021-12-31” in the comparison.

Below is a simple example showing you how to filter out the rows of a pandas DataFrame by a particular date in Python.

import pandas as pd

df = pd.DataFrame({
    "date": ["2021-09-30", "2021-12-31", "2022-03-31", "2022-06-30", "2022-09-30", "2022-12-31"],
    "sales": [100,30,50,60,10,80]
})

df["date"] = pd.to_datetime(df["date"])

df_2022 = df[df["date"] > "2021-12-31"]

print(df_2022)

#Output:
        date  sales
2 2022-03-31     50
3 2022-06-30     60
4 2022-09-30     10
5 2022-12-31     80

Using query() to Filter pandas DataFrame by Date

One useful function which allows you to filter pandas DataFrames is the pandas query() function.

query() allows you to build query strings which can be used to query and filter pandas DataFrames.

You can use the pandas DataFrame query() function to filter out rows of a pandas DataFrame by date.

In this case, the format which you should use for your dates with query() is “YYYYMMDD”.

Below shows you an example of how you can use query() to conditionally remove rows from a pandas DataFrame by date.

import pandas as pd

df = pd.DataFrame({
    "date": ["2021-09-30", "2021-12-31", "2022-03-31", "2022-06-30", "2022-09-30", "2022-12-31"],
    "sales": [100,30,50,60,10,80]
})

df["date"] = pd.to_datetime(df["date"])

df_2022 = df.query("date > 20211231")

print(df_2022)

#Output:
        date  sales
2 2022-03-31     50
3 2022-06-30     60
4 2022-09-30     10
5 2022-12-31     80

Hopefully this article has been useful for you to learn how to filter DataFrames by a date column in pandas.

Categorized in:

Python,

Last Update: March 11, 2024