diff --git a/docs/sphinx/source/changelog/pending.rst b/docs/sphinx/source/changelog/pending.rst index 470f2181..3b602b91 100644 --- a/docs/sphinx/source/changelog/pending.rst +++ b/docs/sphinx/source/changelog/pending.rst @@ -10,5 +10,5 @@ Enhancements Bug fixes --------- -* Set marker linewidth to zero in :py:func:`~rdtools.plotting.degradation_summary_plots` (:pull:`433`) - +* Set marker linewidth to zero in `rdtools.plotting.degradation_summary_plots` (:pull:`433`) +* Fix :py:func:`~rdtools.normalization.energy_from_power` returns incorrect index for shifted hourly data (:issue:`370`, :pull:`437`) diff --git a/rdtools/normalization.py b/rdtools/normalization.py index b1a2ee36..d5afacfc 100644 --- a/rdtools/normalization.py +++ b/rdtools/normalization.py @@ -478,9 +478,9 @@ def _aggregate(time_series, target_frequency, max_timedelta, series_type): ''' # series that has same index as desired output - output_dummy = time_series.resample(target_frequency, - closed='right', - label='right').sum() + output_dummy = time_series.resample( + target_frequency, closed="right", label="right", origin="start" + ).sum() union_index = time_series.index.union(output_dummy.index) time_series = time_series.dropna() @@ -521,13 +521,13 @@ def _aggregate(time_series, target_frequency, max_timedelta, series_type): raise ValueError("series_type must be either 'instantaneous' or 'right_labeled', " "not '{}'".format(series_type)) - series_sum = pd.Series(data=series_sum, index=time_series.index[1:]) + series_sum = pd.Series(data=np.insert(series_sum, 0, np.nan), index=time_series.index) - aggregated = series_sum.resample(target_frequency, - closed='right', - label='right').sum(min_count=1) + aggregated = series_sum.resample( + target_frequency, closed="right", label="right", origin="start" + ).sum(min_count=1) - return aggregated + return aggregated[1:] def _interpolate_series(time_series, target_index, max_timedelta=None, diff --git a/rdtools/test/analysis_chains_test.py b/rdtools/test/analysis_chains_test.py index b93416d6..68a89b26 100644 --- a/rdtools/test/analysis_chains_test.py +++ b/rdtools/test/analysis_chains_test.py @@ -743,6 +743,24 @@ def test_plot_degradation_timeseries(sensor_analysis, clearsky_analysis): ) +def test_energy_from_power_hourly_data(): + + times = pd.date_range("2019-01-01 00:00:00", periods=3, freq="h") + pv = pd.Series([1.2, 2.8, 2.0], index=times) + + energy = normalization.energy_from_power(pv) + pd.testing.assert_series_equal(energy, pv[1:], check_names=False) + + +def test_energy_from_power_shifted_hourly_data(): + + times = pd.date_range("2019-01-01 00:30:00", periods=3, freq="h") + pv = pd.Series([1.2, 2.8, 2.0], index=times) + + energy = normalization.energy_from_power(pv) + pd.testing.assert_series_equal(energy, pv[1:], check_names=False) + + def test_validated_filter_dict_initialization(): valid_keys = ["key1", "key2"] filter_dict = ValidatedFilterDict(valid_keys, key1="value1", key2="value2")