openscm / pymagicc

Python wrapper for the simple climate model MAGICC
https://pymagicc.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
40 stars 27 forks source link

`region_plot` doesn't work #245

Closed rgieseke closed 4 years ago

rgieseke commented 5 years ago

I tried

rcp26.region_plot()

and

rcp26.to_iamdataframe().region_plot()

which failed with

KeyError                                  Traceback (most recent call last)
venv/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2656             try:
-> 2657                 return self._engine.get_loc(key)
   2658             except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'year'
rgieseke commented 5 years ago

Is this due to SCMDataFrame using Timestamps vs. years in Pyam?

znicholls commented 5 years ago

Kind of, it's due to a bug in pyam (hard-coded column name, see https://github.com/IAMconsortium/pyam/blob/25aa7f09fb49ed64569255dd2bf260fb8e62f717/pyam/plotting.py#L243). You'll need something like the below (unless you want to make a PR into pyam which just fixes that line so year is replaced by self.time_col)

plt_df = rcp26.filter(year=XXXX).timeseries()
plt_df["year"] = plt_df["time"].apply(lambda x: x.year)
plt_df = plt_df.drop("time", axis="columns")
plt_scm_df = IamDataFrame(plt_df)
plt_scm_df.region_plot()
znicholls commented 4 years ago

Closing as we use don't use IamDataFrame anymore