8000 Fix off-by-one error in degradation.py by SandraVillamar · Pull Request #339 · NREL/rdtools · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix off-by-one error in degradation.py #339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion docs/sphinx/source/changelog/pending.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ Pending

Bug Fixes
---------
* :py:func:`~rdtools.degradation.degradation_year_on_year` no longer raises
an error for inputs exactly two years long (:pull:`339`)
* :py:func:`~rdtools.plotting.soiling_interval_plot` no longer ignores the optional
``point_color``, ``profile_color``, ``point_alpha``, and ``profile_alpha`` parameters.
(:issue:`343`, :pull:`345`)


Testing
-------
* Added a CI notebook check (:pull:`270`)
Expand All @@ -25,3 +26,10 @@ Requirements
* Bump ``sphinx`` version from 3.2 to 4.5 and ``nbsphinx`` version
from 0.8.5 to 0.8.8 in the optional ``[doc]`` requirements (:pull:`317`, :pull:`325`)
* A number of other requirements updates (:pull:`337`)

Contributors
------------
* Sandra Villamar (:ghuser:`SandraVillamar`)
* Michael Deceglie (:ghuser:`mdeceglie`)
* Chris Deline (:ghuser:`cdeline`)
* Kevin Anderson (:ghuser:`kanderso-nrel`)
19 changes: 14 additions & 5 deletions rdtools/degradation.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,19 @@ def degradation_year_on_year(energy_normalized, recenter=True,
8000 raise ValueError('energy_normalized must not be '
'more frequent than daily')

# Detect less than 2 years of data
if energy_normalized.index[-1] - energy_normalized.index[0] < \
pd.Timedelta('730d'):
raise ValueError('must provide at least two years of '
'normalized energy')
# Detect less than 2 years of data. This is complicated by two things:
# - leap days muddle the precise meaning of "two years of data".
# - can't just check the number of days between the first and last
# index values, since non-daily (e.g. weekly) inputs span
# a longer period than their index values directly indicate.
# See the unit tests for several motivating cases.
if energy_normalized.index.inferred_freq is not None:
step = pd.tseries.frequencies.to_offset(energy_normalized.index.inferred_freq)
else:
step = energy_normalized.index.to_series().diff().median()

if energy_normalized.index[-1] < energy_normalized.index[0] + pd.DateOffset(years=2) - step:
raise ValueError('must provide at least two years of normalized energy')

# Auto center
if recenter:
Expand Down Expand Up @@ -263,6 +271,7 @@ def degradation_year_on_year(energy_normalized, recenter=True,
df.index = df.dt

yoy_result = df.yoy.dropna()

df_right = df.set_index(df.dt_right).drop_duplicates('dt_right')
df['usage_of_points'] = df.yoy.notnull().astype(int).add(
df_right.yoy.notnull().astype(int), fill_value=0)
Expand Down
27 changes: 27 additions & 0 deletions rdtools/test/degradation_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
""" Degradation Module Tests. """

import unittest
import pytest
import sys

import pandas as pd
Expand Down Expand Up @@ -164,6 +165,32 @@ def test_usage_of_points(self):
self.assertTrue((np.sum(rd_result[2]['usage_of_points'])) == 1462)


@pytest.mark.parametrize('start,end,freq', [
('2014-01-01', '2015-12-31', 'D'), # no leap day
('2015-01-01', '2016-12-31', 'D'), # leap day included in index
('2015-01-01', '2016-12-29', '7D'), # leap day in period but not in index
('2016-06-01', '2018-05-31', 'D'), # leap year, but no leap day in period
# ('2016-02-29', '2018-02-28', 'd'), # starts on leap day (doesn't work)
('2014-03-01', '2016-02-29', 'D'), # ends on leap day
('2015-01-01', '2016-12-31', 'M'), # month end
('2015-01-01', '2016-12-31', 'MS'), # month start
])
def test_yoy_two_years_error(start, end, freq):
# GH 339
times = pd.date_range(start, end, freq=freq)
series = pd.Series(1, index=times)
# introduce NaN at the end to ensure that the 2 year requirement applies to
# timestamps, not non-nan values:
series.iloc[-5:] = np.nan
# should not raise an error
_ = degradation_year_on_year(series)
# but if we shorten it by one element, then it should:
with pytest.raises(ValueError, match='must provide at least two years'):
_ = degradation_year_on_year(series.iloc[:-1])
with pytest.raises(ValueError, match='must provide at least two years'):
_ = degradation_year_on_year(series.iloc[1:])


if __name__ == '__main__':
# Initialize logger when run as a module:
# python -m tests.degradation_test
Expand Down
0