Skip to content

Commit

Permalink
Simplify ScenarioInfo.yv_ya using DataFrame.query()
Browse files Browse the repository at this point in the history
  • Loading branch information
khaeru committed Oct 30, 2023
1 parent 695b30e commit 5c2109f
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions message_ix_models/util/scenarioinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,16 @@ 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.
self._yv_ya = pd.DataFrame(
filter(_valid, product(yv, ya)), columns=["year_vtg", "year_act"]
# - Filter only valid years.
self._yv_ya = (
pd.DataFrame(
product(self.set["year"], self.set["year"]),
columns=["year_vtg", "year_act"],
)
.query("@self.y0 <= year_vtg <= year_act")
.reset_index(drop=True)
)

return self._yv_ya
Expand Down

0 comments on commit 5c2109f

Please sign in to comment.