From 90f3c375e3d48dc0cb93979a77e8598686f60d98 Mon Sep 17 00:00:00 2001 From: Paul Natsuo Kishimoto Date: Mon, 30 Oct 2023 11:04:24 +0100 Subject: [PATCH] Simplify ScenarioInfo.yv_ya using DataFrame.query() --- message_ix_models/util/scenarioinfo.py | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/message_ix_models/util/scenarioinfo.py b/message_ix_models/util/scenarioinfo.py index 8025ae1c03..28caccd65b 100644 --- a/message_ix_models/util/scenarioinfo.py +++ b/message_ix_models/util/scenarioinfo.py @@ -132,22 +132,13 @@ def from_url(cls, url: str) -> "ScenarioInfo": def yv_ya(self): """:class:`pandas.DataFrame` with valid ``year_vtg``, ``year_act`` pairs.""" if self._yv_ya is None: - first = self.y0 - - # Product of all years - yv = ya = self.set["year"] - - # Predicate for filtering years - def _valid(elem): - yv, ya = elem - return first <= yv <= ya - # - Cartesian product of all yv and ya. - # - Filter only valid years. # - Convert to data frame. + # - Filter only valid years. self._yv_ya = pd.DataFrame( - filter(_valid, product(yv, ya)), columns=["year_vtg", "year_act"] - ) + product(self.set["year"], self.set["year"]), + columns=["year_vtg", "year_act"], + ).query("@self.y0 <= year_vtg <= year_act") return self._yv_ya