• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / How to Filter pandas DataFrame by Date

How to Filter pandas DataFrame by Date

October 19, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  How to Multiply All Elements in List Using Python
  • 2.  How to Check if a Dictionary is Empty in Python
  • 3.  Using Python to Remove Non-Empty Directory
  • 4.  pandas mean – Get Average of Series or DataFrame Columns
  • 5.  Find All Pythagorean Triples in a Range using Python
  • 6.  Python Check if Object Has Attribute
  • 7.  Append Multiple Elements to List Using Python
  • 8.  Python Even or Odd – Check if Number is Even or Odd Using % Operator
  • 9.  Convert False to 0 in Python
  • 10.  How to Check if Character is Uppercase in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy