Often you may want to filter the rows of a pandas DataFrame by dates. Fortunately this is fairly easy to do and this tutorial explains two ways to do so, depending on the structure of your DataFrame.
Example 1: Filter By Date Using the Index
Suppose we have the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'sales': [4, 11, 13, 9], 'customers': [2, 6, 9, 7]}, index = ['2020-01-15', '2020-01-18', '2020-01-22', '2020-01-24']) #view DataFrame print(df) sales customers 2020-01-15 4 2 2020-01-18 11 6 2020-01-22 13 9 2020-01-24 9 7
Since the dates are in the index of the DataFrame, we can simply use the .loc function to filter the rows based on a date range:
#filter for rows where date is between Jan 15 and Jan 22 df.loc['2020-01-15':'2020-01-22'] sales customers 2020-01-15 4 2 2020-01-18 11 6 2020-01-22 13 9
Note that when we filter the rows using df.loc[start:end] that the dates for start and end are included in the output.
Example 2: Filter By Date Using a Column
Suppose we have the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'sales': [4, 11, 13, 9], 'day': ['2020-01-15', '2020-01-18', '2020-01-22', '2020-01-24']}) #view DataFrame print(df) sales day 0 4 2020-01-15 1 11 2020-01-18 2 13 2020-01-22 3 9 2020-01-24
Since the dates are in one of the columns of the DataFrame, we can instead use boolean operators to filter the rows based on a date range:
#filter for rows where date is after Jan 15 and before Jan 23 df[(df['day'] > '2020-01-15') & (df['day'] < '2020-01-23')] sales day 1 11 2020-01-18 2 13 2020-01-22
Note that we can use similar syntax to filter the rows based on dates outside of some range:
#filter for rows where date is before Jan 20 or after Jan 22 df[(df['day'] < '2020-01-20') | (df['day'] > '2020-01-22')] sales day 0 4 2020-01-15 1 11 2020-01-18 3 9 2020-01-24
Additional Resources
How to Filter a Pandas DataFrame on Multiple Conditions
How to Convert Datetime to Date in Pandas
How to Find Unique Values in Multiple Columns in Pandas