def sector_exposures(self, positions, symbol_sector_map): """ The sector exposures of the portfolio over time. Parameters ---------- positions : pd.DataFrame Contains position values or amounts. - Example index 'AAPL' 'MSFT' 'CHK' cash 2004-01-09 13939.380 -15012.993 -403.870 1477.483 2004-01-12 14492.630 -18624.870 142.630 3989.610 2004-01-13 -13853.280 13653.640 -100.980 100.000 symbol_sector_map : dict or pd.Series Security identifier to sector mapping. Security ids as keys/index, sectors as values. - Example: {'AAPL' : 'Technology' 'MSFT' : 'Technology' 'CHK' : 'Natural Resources'} Returns ------- sector_exp : pd.DataFrame Sectors and their allocations. - Example: index 'Technology' 'Natural Resources' cash 2004-01-09 -1073.613 -403.870 1477.4830 2004-01-12 -4132.240 142.630 3989.6100 2004-01-13 -199.640 -100.980 100.0000 """ sector_alloc = pos.get_sector_exposures(positions, symbol_sector_map) return sector_alloc
def test_sector_exposure(self, positions, mapping, expected_sector_exposure, warning_expected): """ Tests sector exposure mapping and rollup. """ with warnings.catch_warnings(record=True) as w: result_sector_exposure = get_sector_exposures(positions, mapping) assert_frame_equal(result_sector_exposure, expected_sector_exposure) if warning_expected: assert len(w) == 1 else: assert len(w) == 0
def test_sector_exposure(self, positions, mapping, expected_sector_exposure, warning_expected): """ Tests sector exposure mapping and rollup. """ with warnings.catch_warnings(record=True) as w: result_sector_exposure = get_sector_exposures(positions, mapping) assert_frame_equal(result_sector_exposure, expected_sector_exposure) if warning_expected: self.assertEqual(len(w), 1) else: self.assertEqual(len(w), 0)
def test_sector_exposure(self, positions, mapping, expected_sector_exposure, warning_expected): """ Tests sector exposure mapping and rollup. """ with warnings.catch_warnings(record=True) as w: result_sector_exposure = get_sector_exposures(positions, mapping) assert_frame_equal(result_sector_exposure, expected_sector_exposure) # avoids test failure due to DeprecationWarning for pandas>=1.0, <1.1 w_ = [warn for warn in w if issubclass(warn.category, UserWarning)] if warning_expected: self.assertEqual(len(w_), 1) else: self.assertEqual(len(w_), 0)