Skip to content

Commit

Permalink
Fix bug in indexing when interpolate leapday was set
Browse files Browse the repository at this point in the history
If input data is less than 1 year then this failed because it tried to index
days that did not exist
  • Loading branch information
cpaulik committed Mar 8, 2021
1 parent f78018c commit 80389b6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
12 changes: 7 additions & 5 deletions src/pytesmo/time_series/anomaly.py
Expand Up @@ -187,11 +187,6 @@ def calc_climatology(Ser,
clim_ser = pd.Series(clim.values.flatten(),
index=clim.index.values)

if interpolate_leapday and not respect_leap_years:
clim_ser[60] = np.mean((clim_ser[59], clim_ser[61]))
elif interpolate_leapday and respect_leap_years:
clim_ser[366] = np.mean((clim_ser[365], clim_ser[1]))

if wraparound:
index_old = clim_ser.index.copy()
left_mirror = clim_ser.iloc[-moving_avg_clim:]
Expand All @@ -211,5 +206,12 @@ def calc_climatology(Ser,
clim_ser = moving_average(clim_ser, window_size=moving_avg_clim, fillna=fillna, min_obs=min_obs_clim)

clim_ser = clim_ser.reindex(np.arange(366) + 1)

if interpolate_leapday and not respect_leap_years:
clim_ser[60] = np.mean((clim_ser[59], clim_ser[61]))
elif interpolate_leapday and respect_leap_years:
clim_ser[366] = np.mean((clim_ser[365], clim_ser[1]))

clim_ser = clim_ser.fillna(fill)

return clim_ser
8 changes: 7 additions & 1 deletion tests/test_time_series/test_anomaly.py
Expand Up @@ -87,6 +87,12 @@ def test_climatology_always_366():
clim = anomaly.calc_climatology(ts)
assert clim.size == 366

# this should also be the case if interpolate_leapday is set
ts = pd.Series(np.sin(np.arange(10)), index=pd.date_range(
'2000-01-01', freq='D', periods=10))
clim = anomaly.calc_climatology(ts, interpolate_leapday=True)
assert clim.size == 366


def test_climatology_always_366_fill():
ts = pd.Series(np.sin(np.arange(366) / 366. * 2 * np.pi), index=pd.date_range(
Expand Down Expand Up @@ -122,4 +128,4 @@ def test_climatology_interpolate_leapday():
clim = anomaly.calc_climatology(ts, wraparound=True, respect_leap_years=False, fill=-1, interpolate_leapday=True)
assert clim[60] == np.mean((clim[59], clim[61]))
clim = anomaly.calc_climatology(ts, wraparound=True, respect_leap_years=True, fill=-1, interpolate_leapday=True)
assert clim[366] == np.mean((clim[365], clim[1]))
assert clim[366] == np.mean((clim[365], clim[1]))

0 comments on commit 80389b6

Please sign in to comment.