Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Conditional update to a date column in pandas dataframe

For a given table:

df = pd.DataFrame(  {
'datetime': ['2015-01-01', '2015-04-01', '2015-07-01', '2015-12-01', '2015-01-01', '2015-04-01', '2015-07-01', '2015-12-01'],
})
df['datetime'] = pd.to_datetime(df['datetime'])

I would like like to change all the dates that fall on weekends (so weekday==5 or weekday==6) to the Friday before, so something like this:

def adjust_exp_date(x):
    if x.weekday()==5:
        x.weekday() -= 1
    if x.weekday()==6:
        x.weekday() -= 2

df['datetime'].apply(adjust_exp_date)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can sub pd.offsets.Week to adjust to nearest Friday(weekday 4)

m = df['datetime'].dt.weekday.isin([5,6])

df['adjust'] = df['datetime'].mask(m, df['datetime'] - pd.offsets.Week(weekday=4))
     datetime       week  weekday     adjust  adjustweek
0  2015-01-01   Thursday        3 2015-01-01   Thursday
1  2015-01-02     Friday        4 2015-01-02     Friday
2  2015-01-03   Saturday        5 2015-01-02     Friday
3  2015-01-04     Sunday        6 2015-01-02     Friday
4  2015-01-05     Monday        0 2015-01-05     Monday
5  2015-01-06    Tuesday        1 2015-01-06    Tuesday
6  2015-01-07  Wednesday        2 2015-01-07  Wednesday
7  2015-01-08   Thursday        3 2015-01-08   Thursday
8  2015-01-09     Friday        4 2015-01-09     Friday
9  2015-01-10   Saturday        5 2015-01-09     Friday
10 2015-01-11     Sunday        6 2015-01-09     Friday
11 2015-01-12     Monday        0 2015-01-12     Monday
12 2015-01-13    Tuesday        1 2015-01-13    Tuesday
13 2015-01-14  Wednesday        2 2015-01-14  Wednesday
14 2015-01-15   Thursday        3 2015-01-15   Thursday
15 2015-01-16     Friday        4 2015-01-16     Friday
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading