Issue
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
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]'))
Answered By - YfNk
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.