Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pulling certain dates from a dataframe in python

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

like image 823
Ben Pearson Avatar asked Mar 04 '26 18:03

Ben Pearson


1 Answers

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
like image 194
MaxU - stop WAR against UA Avatar answered Mar 06 '26 07:03

MaxU - stop WAR against UA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!