user3822565
user3822565

Reputation: 71

Pandas dataframe filter by date range : today - today+ 1 year

I am able to filter a dataframe using a date range:

df[(df['Due Date'] >= '2017-01-01') & (df['Due Date'] <= '2017-02-01')]

but I would like to be able to filter for a year

Upvotes: 3

Views: 6010

Answers (3)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210842

IIUC you can do it this way:

In [99]: from dateutil.relativedelta import relativedelta

In [100]: today = pd.datetime.today()

In [101]: today_next_year = today + relativedelta(years=1)

In [102]: df.loc[df['Due Date'].between(today, today_next_year)]
Out[102]:
     Due Date  OtherColumn
9  2017-06-30            9
10 2017-09-30           10
11 2017-12-31           11
12 2018-03-31           12

Upvotes: 7

piRSquared
piRSquared

Reputation: 294278

Just to make sure your column is datetime, start with this

df['Due Date'] = pd.to_datetime(df['Due Date'])

Consider the dataframe df

df = pd.DataFrame({
        'Due Date': pd.date_range('2015', periods=20, freq='Q'),
        'OtherColumn': range(20)
    })

you should be able access the year via the dt date accessor

df[df['Due Date'].dt.year >= 2017]

     Due Date  OtherColumn
8  2017-03-31            8
9  2017-06-30            9
10 2017-09-30           10
11 2017-12-31           11
12 2018-03-31           12
13 2018-06-30           13
14 2018-09-30           14
15 2018-12-31           15
16 2019-03-31           16
17 2019-06-30           17
18 2019-09-30           18
19 2019-12-31           19

Or, you can use date filtering on the index

df.set_index('Due Date')['2017']

            OtherColumn
Due Date               
2017-03-31            8
2017-06-30            9
2017-09-30           10
2017-12-31           11

Or

df.set_index('Due Date')['2016':'2017']

            OtherColumn
Due Date               
2016-03-31            4
2016-06-30            5
2016-09-30            6
2016-12-31            7
2017-03-31            8
2017-06-30            9
2017-09-30           10
2017-12-31           11

Upvotes: 3

Grr
Grr

Reputation: 16079

Convert df['Due Date'] to a timestamp and then you can access the year attribute for filtering. For example:

df['Due Date'] = pd.to_datetime(df['Due date'], format='%Y-%m-%d')
df[(df['Due Date'].year >= 2017) & (df['Due Date'].year <= 2018)]

Upvotes: 1

Related Questions