Home > Blockchain >  Calculate next date without saturday
Calculate next date without saturday

Time:01-29

I want to calculate a delivery date where the truck is standing still on saturdays (but not sundays) If the leadtime is two days and I order on friday, the shipment should arrive on monday (sunday monday). The following works great but it excludes sunday:

df['LEV'] = dat   df['Leadtime'].map(pd.tseries.offsets.BusinessDay)

I want to do something like this. But its not working:

df['LEV'] = dat   df['Leadtime'].map(pd.tseries.offsets.CustomBusinessDay(weekmask = 'Mon Tue Wed Thu Fri Sun'))

Where:

2021-05-14 00:00:00
<class 'datetime.datetime'>
<class 'pandas.core.series.Series'>

Thank you

EDIT: Small example:

from datetime import datetime
DatToday = datetime.fromisoformat('2022-01-13')
data = {'Article': ['A1', 'A2'], 'LeadTime': [2, 6], 'DateToday': 
[DatToday,DatToday]}    
df = pd.DataFrame(data)  


df['Delivery_Day'] = DatToday   df['LeadTime'].map(pd.tseries.offsets.BusinessDay)

Current result:

Article    LeadTime  DateToday     Delivery_Day
0     A1    2        2022-01-13    2022-01-17
1     A2    6        2022-01-13    2022-01-21

Wanted result:

Article    LeadTime  DateToday     Delivery_Day
0     A1    2        2022-01-13    2022-01-16
1     A2    6        2022-01-13    2022-01-20

I.e., BusinessDay without sunday.

Error msg:

~\Anaconda3\lib\site-packages\pandas\core\series.py in apply(self, func, 
convert_dtype, args, **kwds)
4198             else:
4199                 values = self.astype(object)._values
-> 4200                 mapped = lib.map_infer(values, f, 
convert=convert_dtype)
4201 
4202         if len(mapped) and isinstance(mapped[0], Series):

pandas\_libs\lib.pyx in pandas._libs.lib.map_infer()

pandas\_libs\tslibs\offsets.pyx in 
pandas._libs.tslibs.offsets.BaseOffset.__call__()

pandas\_libs\tslibs\offsets.pyx in 
pandas._libs.tslibs.offsets.apply_wraps.wrapper()

ApplyTypeError: 

CodePudding user response:

You need to multiply your CustomBusinessDay by the number of days:

cbd = pd.tseries.offsets.CustomBusinessDay(weekmask='Mon Tue Wed Thu Fri Sun')

df['Delivery_Day'] = DatToday   df['LeadTime'] * cbd

output:

  Article  LeadTime  DateToday Delivery_Day
0      A1         2 2022-01-13   2022-01-16
1      A2         6 2022-01-13   2022-01-20
  •  Tags:  
  • Related