Skip to content

Commit

Permalink
Merge branch 'feature/#260-resample-time-series' into features/#276-i…
Browse files Browse the repository at this point in the history
…mprove-simple-example
  • Loading branch information
maike93he committed Aug 2, 2022
2 parents df05a94 + 8bb2a48 commit f64809b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
42 changes: 26 additions & 16 deletions edisgo/network/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -2148,7 +2148,7 @@ def _check_if_components_exist(
def resample_timeseries(self, method: str = "ffill", freq: str = "15min"):
"""
Returns timeseries resampled from hourly resolution to 15 minute resolution.
#ToDo: Adjust docstring
Parameters
----------
method : str, optional
Expand All @@ -2174,27 +2174,36 @@ def resample_timeseries(self, method: str = "ffill", freq: str = "15min"):
"""
attrs = self._attributes
freq_orig = self.timeindex[1] - self.timeindex[0]
df_dict = {}
for attr in attrs:
df_dict[attr] = getattr(self, attr)
new_dates = pd.DatetimeIndex(
[
df_dict[attr].index[-1] + pd.DateOffset(hours=1)
] # ToDo: For downsampling the Offset has to be adjusted
)
if pd.Timedelta(freq) < freq_orig: # up-sampling
new_dates = pd.DatetimeIndex(
[df_dict[attr].index[-1] + freq_orig] # pd.DateOffset(hours=1)
)
else: # down-sampling
new_dates = pd.DatetimeIndex([df_dict[attr].index[-1]])
df_dict[attr] = (
df_dict[attr]
.reindex(df_dict[attr].index.union(new_dates).unique().sort_values())
.ffill()
)
index = pd.date_range(
self.timeindex[0],
self.timeindex[-1] + pd.Timedelta("1h"),
freq=freq,
closed="left",
)
if pd.Timedelta(freq) < freq_orig: # up-sampling
index = pd.date_range(
self.timeindex[0],
self.timeindex[-1] + freq_orig,
freq=freq,
closed="left",
)
else: # down-sampling
index = pd.date_range(
self.timeindex[0],
self.timeindex[-1],
freq=freq,
)
self._timeindex = index
if pd.Timedelta(index[1] - index[0]) < pd.Timedelta("1h"):
if pd.Timedelta(freq) < freq_orig: # up-sampling
if method == "interpolate":
for attr in attrs:
setattr(
Expand All @@ -2207,19 +2216,20 @@ def resample_timeseries(self, method: str = "ffill", freq: str = "15min"):
setattr(
self, attr, df_dict[attr].resample(freq, closed="left").ffill()
)
else:
elif method == "bfill":
for attr in attrs:
setattr(
self, attr, df_dict[attr].resample(freq, closed="left").bfill()
)
else:
else: # ToDo: Logger Warning: method not implemented
print(" ")
else: # down-sampling
for attr in attrs:
setattr(
self,
attr,
df_dict[attr].resample(freq).mean(),
)
print(" ")


class TimeSeriesRaw:
Expand Down
6 changes: 6 additions & 0 deletions tests/network/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -2329,6 +2329,12 @@ def test_resample_timeseries(self):
assert (
self.edisgo.timeseries.generators_active_power.mean() == mean_value_orig
).unique()
# Same tests for down-sampling
self.edisgo.timeseries.resample_timeseries(freq="2h")
assert len(self.edisgo.timeseries.timeindex) == 0.5 * len_timeindex_orig
assert (
self.edisgo.timeseries.generators_active_power.mean() == mean_value_orig
).unique()


class TestTimeSeriesRaw:
Expand Down

0 comments on commit f64809b

Please sign in to comment.