I am trying to create a function that I will .apply() to a dataframe to:
I have simplified the logic of my function to iron out this issue - I will add more logic later.
My function:
def test(trade_date):
if (trade_date - BDay(1)).isin(pricing_date):
return True
else:
return False
Error:
AttributeError: 'Timestamp' object has no attribute 'isin'
Looks like there is an issue with using .isin with a Timestamp. However, when I run code to test within the dataframe itself:
df['Check'] = df['test_date'].isin(pricing_date)
The expected output is returned - isin() does work properly with this data.
TradeDate
2023-01-03 False
2023-01-03 False
2023-01-03 False
2023-01-03 False
2023-01-03 False
...
2023-03-22 True
2023-03-22 True
2023-03-22 True
2023-03-22 True
2023-03-22 True
Name: Check, Length: 14324, dtype: bool
The column that .isin() is being called on is of datatype: datetime64[ns], but unsure how to convert the timestamp in my function to this data type - I have read in many places that they are virtually equivalent, just types from python vs pandas.
Name: test_date, Length: 14324, dtype: datetime64[ns]
Any help is appreciated!
Tried passing in a timestamp into .isin - expected output from running it directly on the dataframe.
Pandas dataframe apply
runs the function on all the values inside the pd.Series
rather than the function in the pd.Series
. Thus, trade_date
would be a timestamp, that doesn't have the isin
method. What you should do is something like this:
def test(trade_date):
return (trade_date - BDay(1)) in pricing_date
Or, much simpler:
df['Check'] = (df['test_date']-BDay(1)).isin(pricing_date)