code: def test_missing_stocks_and_dates(self):
      (
          returns,
          positions,
          factor_returns,
          factor_loadings,
      ) = generate_toy_risk_model_output()
  
      # factor loadings missing a stock should raise a warning
      factor_loadings_missing_stocks = factor_loadings.drop(
          "TLT", level="ticker"
      )
  
      with warnings.catch_warnings(record=True) as w:
          warnings.simplefilter("always", UserWarning)
  
          perf_attrib(
              returns,
              positions,
              factor_returns,
              factor_loadings_missing_stocks,
          )
  
          self.assertEqual(len(w), 1)
          self.assertIn(
              "The following assets were missing factor loadings: "
              "['TLT']",
              str(w[-1].message),
          )
          self.assertIn(
              "Ratio of assets missing: 0.333", str(w[-1].message)
          )
  
          # missing dates should raise a warning
          missing_dates = ["2017-01-01", "2017-01-05"]
          factor_loadings_missing_dates = factor_loadings.drop(
              missing_dates
          )
  
          exposures, perf_attrib_data = perf_attrib(
              returns,
              positions,
              factor_returns,
              factor_loadings_missing_dates,
          )
  
          self.assertEqual(len(w), 2)
          self.assertIn(
              "Could not find factor loadings for "
              "{} dates".format(len(missing_dates)),
              str(w[-1].message),
          )
  
          for date in missing_dates:
              self.assertNotIn(date, exposures.index)
              self.assertNotIn(date, perf_attrib_data.index)
  
          # perf attrib should work if factor_returns already missing dates
          exposures, perf_attrib_data = perf_attrib(
              returns,
              positions,
              factor_returns.drop(pd.DatetimeIndex(missing_dates)),
              factor_loadings_missing_dates,
          )
  
          self.assertEqual(len(w), 3)
          self.assertIn(
              "Could not find factor loadings for "
              "{} dates".format(len(missing_dates)),
              str(w[-1].message),
          )
  
          for date in missing_dates:
              self.assertNotIn(date, exposures.index)
              self.assertNotIn(date, perf_attrib_data.index)
  
          # test missing stocks and dates
          factor_loadings_missing_both = (
              factor_loadings_missing_dates.drop("TLT", level="ticker")
          )
  
          exposures, perf_attrib_data = perf_attrib(
              returns,
              positions,
              factor_returns,
              factor_loadings_missing_both,
          )
  
          self.assertEqual(len(w), 5)
          self.assertIn(
              "The following assets were missing factor loadings: "
              "['TLT']",
              str(w[-2].message),
          )
          self.assertIn(
              "Ratio of assets missing: 0.333", str(w[-2].message)
          )
  
          self.assertIn(
              "Could not find factor loadings for "
              "{} dates".format(len(missing_dates)),
              str(w[-1].message),
          )
          for date in missing_dates:
              self.assertNotIn(date, exposures.index)
              self.assertNotIn(date, perf_attrib_data.index)
  
          # raise exception if all stocks are filtered out
          empty_factor_loadings = factor_loadings.drop(
              ["AAPL", "TLT", "XOM"], level="ticker"
          )
  
          with self.assertRaisesRegexp(
              ValueError, "No factor loadings were available"
          ):
              exposures, perf_attrib_data = perf_attrib(
                  returns,
                  positions,
                  factor_returns,
                  empty_factor_loadings,
              )
  
file_path: pyfolio/pyfolio/tests/test_perf_attrib.py
imports: import numpy
  import pandas
  import unittest
  import warnings
  import empyrical
  from pyfolio.perf_attrib import perf_attrib, create_perf_attrib_stats, _cumulative_returns_less_costs
label_desc: Use image node_id 3 for calling the PerfAttribTestCase obj's underlying member method code with example usage: obj.test_missing_stocks_and_dates() without return types