pmorissette / ffn

ffn - a financial function library for Python
pmorissette.github.io/ffn
MIT License
1.9k stars 284 forks source link

calc_stat traceback error #180

Closed pinpss closed 2 years ago

pinpss commented 2 years ago

Hello, I want to calculate my performance statistics, however, calc_stats() returns an error related to an ambiguous NA value. But I am not sure how to solve this.

Here is the error. `TypeError Traceback (most recent call last) Input In [332], in <cell line: 2>() 1 # ok now what about some performance metrics? ----> 2 stats = pd.DataFrame(prices).calc_stats() 3 stats.display()

File ~/opt/anaconda3/lib/python3.8/site-packages/ffn/core.py:1228, in calc_stats(prices) 1226 return PerformanceStats(prices) 1227 elif isinstance(prices, pd.DataFrame): -> 1228 return GroupStats(*[prices[x] for x in prices.columns]) 1229 else: 1230 raise NotImplementedError("Unsupported type")

File ~/opt/anaconda3/lib/python3.8/site-packages/ffn/core.py:837, in GroupStats.init(self, *prices) 835 self._end = self._prices.index[-1] 836 # calculate stats for entire series --> 837 self._update(self._prices)

File ~/opt/anaconda3/lib/python3.8/site-packages/ffn/core.py:847, in GroupStats._update(self, data) 846 def _update(self, data): --> 847 self._calculate(data) 848 self._update_stats()

File ~/opt/anaconda3/lib/python3.8/site-packages/ffn/core.py:854, in GroupStats._calculate(self, data) 852 for c in data.columns: 853 prc = data[c] --> 854 self[c] = PerformanceStats(prc)

File ~/opt/anaconda3/lib/python3.8/site-packages/ffn/core.py:72, in PerformanceStats.init(self, prices, rf) 68 self._end = self.prices.index[-1] 70 self.rf = rf ---> 72 self._update(self.prices)

File ~/opt/anaconda3/lib/python3.8/site-packages/ffn/core.py:91, in PerformanceStats._update(self, obj) 89 def _update(self, obj): 90 # calc ---> 91 self._calculate(obj) 93 # update derived structure 94 # return table as dataframe for easier manipulation 95 self.return_table = pd.DataFrame(self.return_table).T

File ~/opt/anaconda3/lib/python3.8/site-packages/ffn/core.py:320, in PerformanceStats._calculate(self, obj) 305 if idx.year not in self.return_table: 306 self.return_table[idx.year] = { 307 1: 0, 308 2: 0, (...) 318 12: 0, 319 } --> 320 if not np.isnan(mr[idx]): 321 self.return_table[idx.year][idx.month] = mr[idx] 322 # add first month

File ~/opt/anaconda3/lib/python3.8/site-packages/pandas/_libs/missing.pyx:446, in pandas._libs.missing.NAType.bool()

TypeError: boolean value of NA is ambiguous`