diff --git a/edisgo/edisgo.py b/edisgo/edisgo.py index 07324d01..7a7bd42f 100755 --- a/edisgo/edisgo.py +++ b/edisgo/edisgo.py @@ -557,10 +557,28 @@ def set_time_series_active_power_predefined( :py:attr:`~.network.timeseries.TimeSeries.timeindex` is used. If :py:attr:`~.network.timeseries.TimeSeries.timeindex` is not set, the data is indexed using a default year and set for the whole year. + In this case, the EDisGo TimeSeries timeindex is automatically set to + the selected default year so that imported data is linked to a valid + time index. """ engine = kwargs["engine"] if "engine" in kwargs else egon_engine() - if self.timeseries.timeindex.empty: + if self.timeseries.timeindex.empty and kwargs.get("timeindex", None) is None: + if conventional_loads_ts == "oedb": + default_year = tools.get_year_based_on_scenario(kwargs.get("scenario")) + if default_year is None: + default_year = 2011 + else: + default_year = 2011 + self.timeseries.timeindex = pd.date_range( + f"1/1/{default_year}", periods=8760, freq="H" + ) + logger.warning( + "No timeindex was set. TimeSeries.timeindex is automatically " + f"set to the default year {default_year} to match imported " + "time series." + ) + elif self.timeseries.timeindex.empty: logger.warning( "When setting time series using predefined profiles it is better to " "set a time index as all data in TimeSeries class is indexed by the" diff --git a/tests/test_edisgo.py b/tests/test_edisgo.py index f20642f0..ac84ecb1 100755 --- a/tests/test_edisgo.py +++ b/tests/test_edisgo.py @@ -164,6 +164,21 @@ def test_set_time_series_manual(self, caplog): storage_units_ts, self.edisgo.timeseries.storage_units_reactive_power ) + def test_set_time_series_active_power_predefined_oedb_auto_sets_timeindex(self): + # Test that timeindex is automatically set when importing predefined time series + # and no timeindex is provided + edisgo = EDisGo(ding0_grid=pytest.ding0_test_network_path) + # Ensure timeindex is empty initially + assert edisgo.timeseries.timeindex.empty + # Call set_time_series_active_power_predefined with demandlib data (local) + edisgo.set_time_series_active_power_predefined( + conventional_loads_ts="demandlib", + ) + # Check that timeindex is now set to the default year (2011) with 8760 hours + assert not edisgo.timeseries.timeindex.empty + assert edisgo.timeseries.timeindex.year.unique()[0] == 2011 + assert len(edisgo.timeseries.timeindex) == 8760 + def test_set_time_series_worst_case_analysis(self): self.edisgo.set_time_series_worst_case_analysis( cases="load_case", generators_names=["Generator_1"], loads_names=[] @@ -286,6 +301,29 @@ def test_set_time_series_active_power_predefined_oedb(self): len(fluctuating_gens), ) + @pytest.mark.slow + def test_set_time_series_active_power_predefined_oedb_auto_sets_timeindex( + self, + ): + edisgo_object = EDisGo( + ding0_grid=pytest.ding0_test_network_3_path, + legacy_ding0_grids=False, + ) + + edisgo_object.set_time_series_active_power_predefined( + conventional_loads_ts="oedb", + fluctuating_generators_ts="oedb", + scenario="eGon2035", + engine=pytest.engine, + conventional_loads_names=[ + "Load_mvgd_33535_lvgd_1164210000_244_residential" + ], + ) + + assert not edisgo_object.timeseries.timeindex.empty + assert edisgo_object.timeseries.timeindex[0].year == 2035 + assert edisgo_object.timeseries.timeindex.shape == (8760,) + def test_set_time_series_reactive_power_control(self): # set active power time series for fixed cosphi timeindex = pd.date_range("1/1/1970", periods=3, freq="H")