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

Calculating number of business days between two dates in pandas

I have a following problem. I would like to calculate number of business days between two dates. Example:

import numpy as np

pokus = {"start_date" : "2022-01-01 10:00:00" , "end_date" : "2022-01-01 17:00:00" }
df = pd.DataFrame(pokus, index=[0])
cas_df["bus_days"] = np.busday_count(pd.to_datetime(df["start_date"]) , pd.to_datetime(df["end_date"]))

which returns a confusing error

Traceback (most recent call last):
  File "/home/vojta/.local/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3251, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-17-8910d714721b>", line 3, in <module>
    cas_df["bus_days"] = np.busday_count(pd.to_datetime(df["start_date"]) , pd.to_datetime(df["end_date"]))
  File "<__array_function__ internals>", line 180, in busday_count
TypeError: Iterator operand 0 dtype could not be cast from dtype('<M8[ns]') to dtype('<M8[D]') according to the rule 'safe'

How can I fix it please? Thanks

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 :

try this :

cas_df["bus_days"] = np.busday_count(pd.to_datetime(df["start_date"]).values.astype('datetime64[D]') , pd.to_datetime(df["end_date"]).values.astype('datetime64[D]'))
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