AI4S2S / s2spy

A high-level python package integrating expert knowledge and artificial intelligence to boost (sub) seasonal forecasting
https://ai4s2s.readthedocs.io/
Apache License 2.0
20 stars 7 forks source link

Calendar visualization not working with `CustomCalendar` only has precursor periods #128

Closed geek-yang closed 1 year ago

geek-yang commented 1 year ago

The CustomCalendar works with only precursor periods or target periods being provided. However, with precursor periods only, when calling 'visualize', it gives an error:

KeyError                                  Traceback (most recent call last)
File ~/miniconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py:3621, in Index.get_loc(self, key, method, tolerance)
   3620 try:
-> 3621     return self._engine.get_loc(casted_key)
   3622 except KeyError as err:

File ~/miniconda3/lib/python3.8/site-packages/pandas/_libs/index.pyx:136, in pandas._libs.index.IndexEngine.get_loc()

File ~/miniconda3/lib/python3.8/site-packages/pandas/_libs/index.pyx:163, in pandas._libs.index.IndexEngine.get_loc()

File pandas/_libs/hashtable_class_helper.pxi:2131, in pandas._libs.hashtable.Int64HashTable.get_item()

File pandas/_libs/hashtable_class_helper.pxi:2140, in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 1

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
/home/yangliu/AI4S2S/s2spy/notebooks/tutorial_alternative_calendars.ipynb Cell 23 in <cell line: 1>()
----> [1](vscode-notebook-cell://wsl%2Bubuntu-20.04/home/yangliu/AI4S2S/s2spy/notebooks/tutorial_alternative_calendars.ipynb#X31sdnNjb2RlLXJlbW90ZQ%3D%3D?line=0) calendar.visualize(relative_dates=True, add_length=True)

File ~/AI4S2S/s2spy/s2spy/_base_calendar.py:361, in BaseCalendar.visualize(self, n_years, relative_dates, add_length)
    359 n_years = max(n_years, 1)
...
   3626     #  InvalidIndexError. Otherwise we fall through and re-raise
   3627     #  the TypeError.
   3628     self._check_indexing_error(key)

KeyError: 1

The visualization works when the CustomCalendar only has target periods.

BSchilperoort commented 1 year ago

Fixed in #113. It is also tested for now.