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 7979973 + d3d1355 commit df05a94
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
30 changes: 30 additions & 0 deletions edisgo/edisgo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2037,6 +2037,36 @@ def check_integrity(self):

logging.info("Integrity check finished. Please pay attention to warnings.")

def resample_timeseries(self, method: str = "ffill", freq: str = "15min"):
"""
Returns timeseries resampled from hourly resolution to 15 minute resolution.
Parameters
----------
method : str, optional
Method to choose from to fill missing values when upsampling. Possible
options are:
* 'ffill': propagate last valid observation forward to next valid
observation. See
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.ffill.html
'ffill' is the Default.
* 'bfill': use next valid observation to fill gap. See
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.bfill.html
* 'interpolate': Fill NaN values using an interpolation method. See
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.interpolate.html
freq : str, optional
Frequency that timeseries is resampled to. Can be any frequency up to one
hour. Offset aliases can be found here:
https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases
15 minutes is the default.
"""
self.timeseries.resample_timeseries(method=method, freq=freq)


def import_edisgo_from_pickle(filename, path=""):
abs_path = os.path.abspath(path)
Expand Down
76 changes: 76 additions & 0 deletions edisgo/network/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -2145,6 +2145,82 @@ def _check_if_components_exist(
return set(component_names) - set(comps_not_in_network)
return component_names

def resample_timeseries(self, method: str = "ffill", freq: str = "15min"):
"""
Returns timeseries resampled from hourly resolution to 15 minute resolution.
Parameters
----------
method : str, optional
Method to choose from to fill missing values when upsampling. Possible
options are:
* 'ffill': propagate last valid observation forward to next valid
observation. See
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.ffill.html
'ffill' is the Default.
* 'bfill': use next valid observation to fill gap. See
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.bfill.html
* 'interpolate': Fill NaN values using an interpolation method. See
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.interpolate.html
freq : str, optional
Frequency that timeseries is resampled to. Can be any frequency up to one
hour. Offset aliases can be found here:
https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases
15 minutes is the default.
"""
attrs = self._attributes
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
)
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",
)
self._timeindex = index
if pd.Timedelta(index[1] - index[0]) < pd.Timedelta("1h"):
if method == "interpolate":
for attr in attrs:
setattr(
self,
attr,
df_dict[attr].resample(freq, closed="left").interpolate(),
)
elif method == "ffill":
for attr in attrs:
setattr(
self, attr, df_dict[attr].resample(freq, closed="left").ffill()
)
else:
for attr in attrs:
setattr(
self, attr, df_dict[attr].resample(freq, closed="left").bfill()
)
else:
for attr in attrs:
setattr(
self,
attr,
df_dict[attr].resample(freq).mean(),
)
print(" ")


class TimeSeriesRaw:
"""
Expand Down
20 changes: 20 additions & 0 deletions tests/network/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -2310,6 +2310,26 @@ def test_check_if_components_exist(self):
assert len(component_names) == 1
assert "Load_residential_LVGrid_5_3" in component_names

def test_resample_timeseries(self):
# add dummy time series
timeindex = pd.date_range("1/1/2011", periods=4, freq="H")
self.edisgo.set_timeindex(timeindex)
# add example data for active power
self.edisgo.set_time_series_active_power_predefined(
fluctuating_generators_ts="oedb"
)
len_timeindex_orig = len(self.edisgo.timeseries.timeindex)
mean_value_orig = self.edisgo.timeseries.generators_active_power.mean()
self.edisgo.timeseries.resample_timeseries()
# check if resampled length of time index is 4 times original length of
# timeindex
assert len(self.edisgo.timeseries.timeindex) == 4 * len_timeindex_orig
# check if mean value of resampled data is the same as mean value of original
# data
assert (
self.edisgo.timeseries.generators_active_power.mean() == mean_value_orig
).unique()


class TestTimeSeriesRaw:
@pytest.fixture(autouse=True)
Expand Down

0 comments on commit df05a94

Please sign in to comment.