3

I am using pandas to clean a database and I have a list of dates all in format like 08-Jun-2017 , 12-Jun-2017 etc within a dataframe. I would like to pull out all the rows where the date is less than 14 days from the current date. Thanks

4
  • 1
    are the dates in string format or in a datetime Commented May 5, 2017 at 9:17
  • 1
    You have to provide a minimal reproducible example. Commented May 5, 2017 at 9:18
  • @IanS 'Have' is a strong word... It is recommended, because it will help get better answers. Commented May 5, 2017 at 9:23
  • @Jblasco agreed, it can also help not attract downvotes for lack of research :) Commented May 5, 2017 at 9:59

1 Answer 1

3

Demo:

In [118]: df = pd.DataFrame({'date': pd.date_range(end='2017-05-05', freq='9D', periods=20)}) \
                 .sample(frac=1).reset_index(drop=True)

In [119]: df
Out[119]:
         date
0  2016-11-15
1  2017-03-30
2  2017-01-17
3  2017-04-17
4  2017-03-12
5  2017-02-22
6  2017-01-08
7  2017-04-26
8  2017-05-05
9  2016-12-03
10 2017-03-03
11 2016-12-21
12 2017-02-04
13 2017-04-08
14 2017-03-21
15 2016-11-24
16 2017-01-26
17 2016-12-30
18 2017-02-13
19 2016-12-12

In [120]: df.loc[df.date > pd.datetime.now() - pd.Timedelta('14 days')]
Out[120]:
        date
7 2017-04-26
8 2017-05-05

the same solution, but for dates (as strings):

In [122]: df['dt_str'] = df.date.dt.strftime('%d-%b-%Y')

In [123]: df
Out[123]:
         date       dt_str
0  2016-11-15  15-Nov-2016
1  2017-03-30  30-Mar-2017
2  2017-01-17  17-Jan-2017
3  2017-04-17  17-Apr-2017
4  2017-03-12  12-Mar-2017
5  2017-02-22  22-Feb-2017
6  2017-01-08  08-Jan-2017
7  2017-04-26  26-Apr-2017
8  2017-05-05  05-May-2017
9  2016-12-03  03-Dec-2016
10 2017-03-03  03-Mar-2017
11 2016-12-21  21-Dec-2016
12 2017-02-04  04-Feb-2017
13 2017-04-08  08-Apr-2017
14 2017-03-21  21-Mar-2017
15 2016-11-24  24-Nov-2016
16 2017-01-26  26-Jan-2017
17 2016-12-30  30-Dec-2016
18 2017-02-13  13-Feb-2017
19 2016-12-12  12-Dec-2016

In [124]: df.loc[pd.to_datetime(df['dt_str'], errors='coerce') >= pd.datetime.now() - pd.Timedelta('14 days')]
Out[124]:
        date       dt_str
7 2017-04-26  26-Apr-2017
8 2017-05-05  05-May-2017
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.