'Function with dates
I created a function with the objective that converts the value of Colombian pesos to dollars depending on the date of the index in a dataframe:
def pesosdolar(fecha):
fecha=pd.to_datetime(fecha)
plus_one_day = fecha + timedelta(days=1)
minus_one_day= fecha - timedelta(days=1)
try:
copusd = yf.Ticker("USDCOP=X").history(start=fecha, end=fecha)
return copusd['Close'].iloc[0]
except IndexError:
alt = yf.Ticker("USDCOP=X").history(start=fecha - timedelta(days=3), end=fecha - timedelta(days=1))
return alt['Close'].mean()
when I write a simple date ('2022-04-28' for example) it works perfect, the problem is when I try to put the dataframe
pesosdolar(df_ordenes['createdAt'])
The warning:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Further, this does not work:
df_ordenes = df_ordenes.assign(dolar = lambda x: (x['amount_in'] / pesosdolar(x['createdAt'])))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
