Skip to content

KeyError if target data have invalid values #250

@babetsdasha

Description

@babetsdasha

Hi, everyone. IDataProviderTarget have optional parameter intensity_metric, but if target_type="int" and target doesn't have intensity_metric, we are getting an error (in case that we have only 1 target)

KeyError: "None of ['company_id', 'time_frame', 'scope'] are in the columns"

here
https://github.com/OFBDABV/SBTi/blob/master/SBTi/target_validation.py#L40

I have a few suggestions how it can be fixed

  • extend IDataProviderTarget validation
  • put default value for intensity_metric

Here is test for reproduce the issue

class FailedTest(unittest.TestCase):
    def setUp(self):
        company_id = "BaseCompany"
        self.BASE_COMP_SCORE = 0.43
        self.company_base = IDataProviderCompany(
            company_name=company_id,
            company_id=company_id,
            ghg_s1s2=100,
            ghg_s3=0,
            company_revenue=100,
            company_market_cap=100,
            company_enterprise_value=100,
            company_total_assets=100,
            company_cash_equivalents=100,
            isic='A12'
        )

        self.target_base = IDataProviderTarget(
            company_id=company_id,
            target_type="int",  # here the changes
            scope=EScope.S1S2,
            coverage_s1=0.95,
            coverage_s2=0.95,
            coverage_s3=0,
            reduction_ambition=0.8,
            base_year=2019,
            base_year_ghg_s1=100,
            base_year_ghg_s2=0,
            base_year_ghg_s3=0,
            end_year=2030,
        )

        self.pf_base = PortfolioCompany(
            company_name=company_id,
            company_id=company_id,
            investment_value=100,
            company_isin=company_id,
        )

    def test_basic(self):
        company = copy.deepcopy(self.company_base)
        target = copy.deepcopy(self.target_base)
        data_provider = TestDataProvider(companies=[company], targets=[target])

        temp_score = TemperatureScore(
            time_frames=[ETimeFrames.MID, ETimeFrames.SHORT, ETimeFrames.LONG],
            scopes=[EScope.S1S2],
            aggregation_method=PortfolioAggregationMethod.WATS,
        )

        pf_company = copy.deepcopy(self.pf_base)

        # here exception
        portfolio_data = SBTi.utils.get_data([data_provider], [pf_company])

        scores = temp_score.calculate(portfolio_data)
        self.assertIsNotNone(scores)
        self.assertEqual(len(scores.index), 3)


if __name__ == "__main__":
    test = FailedTest()
    test.setUp()
    test.test_basic()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions