Empyrial icon indicating copy to clipboard operation
Empyrial copied to clipboard

Can't use empyrial function when specifying rebalance

Open teegin-g opened this issue 7 months ago • 6 comments

Describe the bug When I specify the rebalance parameter when creating a portfolio object, the empyrial function will return "ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."

To Reproduce

  1. I define portfolio as such from empyrial import empyrial, Engine import pandas as pd portfolio = Engine( start_date = "2018-08-01", portfolio = ["BABA", "PDD", "KO", "AMD","^IXIC"], optimizer = "EF", # rebalance every year rebalance = '1y' )
  2. I run the empyrial function and it returns an error. empyrial(portfolio)

ValueError Traceback (most recent call last) Cell In[39], line 1 ----> 1 empyrial(portfolio)

File ~\anaconda3\envs\myenv\lib\site-packages\empyrial\main.py:192, in empyrial(my_portfolio, rf, sigma_value, confidence_value, report, filename) 191 def empyrial(my_portfolio, rf=0.0, sigma_value=1, confidence_value=0.95, report=False, filename="empyrial_report.pdf"): --> 192 if my_portfolio.rebalance != None: 193 # we want to get the dataframe with the dates and weights 194 rebalance_schedule = my_portfolio.rebalance 196 columns = []

File ~\anaconda3\envs\myenv\lib\site-packages\pandas\core\generic.py:1527, in NDFrame.nonzero(self) 1525 @final 1526 def nonzero(self) -> NoReturn: -> 1527 raise ValueError( 1528 f"The truth value of a {type(self).name} is ambiguous. " 1529 "Use a.empty, a.bool(), a.item(), a.any() or a.all()." 1530 )

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Expected behavior However, when I remove the rebalance parameter, I get the expected output: metrics, graphs, everything, ex: from empyrial import empyrial, Engine import pandas as pd portfolio = Engine( start_date = "2018-08-01", portfolio = ["BABA", "PDD", "KO", "AMD","^IXIC"], optimizer = "EF" ) empyrial(portfolio)

Other details I found that if I modify this part: if my_portfolio.rebalance != None: [...] to if isinstance(my_portfolio.rebalance, pd.DataFrame): [...] It seems to work properly?

I'm running this via dataspell in an anaconda environment (2.5.0) on a windows PC. I'm an economics student and this is my first github post so let me know if you need any additional information

teegin-g avatar Nov 30 '23 19:11 teegin-g