0

I would like to subset a data frame based on a date column, which originally has this format:

3/22/13

After I transform it to a date:

df['date']=pd.to_datetime(df['date'], format='%m/%d/%y')

I get this:

2013-03-22 00:00:00

Now I would like to subset it with something like this:

 df.loc[(df['date']>'2014-06-22')]

But that either gives me an empty data frame or full data frame, that is no filtering.

Any suggestions how I can get this to work?

remark: I am well aware that similar questions have been asked in other forums but I could not figure out a solution since my date column looks different.

2
  • Your code should working, can you test between like df.loc[df['date'].between('2013-03-22', '2013-06-22')] ? Commented Apr 25, 2018 at 5:10
  • jesrael, your code also gave me the same problem, i.e. an empty dataset. @Scott: Yeah, the dataset goes from 2013 to 2018 Commented Apr 25, 2018 at 15:28

1 Answer 1

5

First you have to convert your starting date and final date into a datetime format. Then you can apply multiple conditions inside df.loc. Do not forget to reassign your modifications to your df :

import pandas as pd
from datetime import datetime

df['date']=pd.to_datetime(df['date'], format='%m/%d/%y')

date1 = datetime.strptime('2013-03-23', '%Y-%m-%d')
date2 = datetime.strptime('2013-03-25', '%Y-%m-%d')

df = df.loc[(df['date']>date1) & (df['date']<date2)]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.