Skip to content
Advertisement

Passing datetime64[ns] from pandas’ data frame as an argument to a function

I’m trying to create an additional column in a data frame to show the number of network days (excluding custom holidays) between two dates. I’m using a function to which I’m trying to pass dates from df‘s columns as arguments, but I can’t make it work.

Below is my code (I’m using two made-up holidays in the given set):

JavaScript

The formula itself works fine:

JavaScript

3

Minimal data frame with the dtypes I’m working on:

JavaScript

When I’m trying the below way…

JavaScript

…I’m getting an error:

AttributeError: ‘Series’ object has no attribute ‘days’

I’ve also tried to use numpy:

JavaScript

I got an error as well:

AttributeError: ‘numpy.timedelta64’ object has no attribute ‘days’

Could you point me in the right direction?

EDIT: The correct answer to my problem is @Kris’s last comment.

IMPORTANT! Although the lambda doesn’t return any errors, it takes public_holidays into consideration correctly in 2 scenarios:

A) The elements of public_holidays are of class datetime.date and df‘s dates are of class object (I got this by removing pd.to_datetime() lines from the code).

B) The public_holidays is of type list (created from an Excel table via public_holidays = df_ph['Date'].tolist()), its elements are of class Timestamp and pd.to_datetime() lines are not removed from the code above (making dates in df datetime64[ns]).

Advertisement

Answer

As per my comment, use .apply:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement