Skip to content

Commit

Permalink
Allow tz-aware timstamps in gaps.trim (#206)
Browse files Browse the repository at this point in the history
* updated gaps file for later versions of pandas

* fixed tz issues

* included new note for gaps function

* included notes and unit testing for gaps.trim

* Fixed pep8 format

* deleted .date()

---------

Co-authored-by: Nguyen <qnguyen@nrel.gov>
  • Loading branch information
qnguyen345 and Nguyen committed Feb 9, 2024
1 parent 75df0ad commit 201b495
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/whatsnew/0.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Bug Fixes
of raising ``AttributeError``. (:pull:`181`)
* Compatibility with pandas 2.0.0 (:pull:`185`) and future versions of pandas (:pull:`203`)
* Compatibility with scipy 1.11 (:pull:`196`)
* Updated function :py:func:`~pvanalytics.quality.gaps.trim` to handle pandas 2.0.0 update for tz-aware timeseries
(:pull:`206`)

Requirements
~~~~~~~~~~~~
Expand All @@ -64,3 +66,4 @@ Contributors
* Kevin Anderson (:ghuser:`kanderso-nrel`)
* Cliff Hansen (:ghuser:`cwhanse`)
* Abhishek Parikh (:ghuser:`abhisheksparikh`)
* Quyen Nguyen (:ghuser:`qnguyen345`)
3 changes: 2 additions & 1 deletion pvanalytics/quality/gaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,9 @@ def trim(series, days=10):
"""
start, end = start_stop_dates(series, days=days)
mask = pd.Series(False, index=series.index)

if start:
mask.loc[start.date():end.date()] = True
mask.loc[start:end] = True
return mask


Expand Down
21 changes: 21 additions & 0 deletions pvanalytics/tests/quality/test_gaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,27 @@ def test_trim_daily_index():
)


def test_trim_daily_index_tz_aware():
"""trim works when data has a daily index and data is tz-aware."""
data = pd.Series(True, index=pd.date_range(
start='1/1/2020', end='2/29/2020', freq='D', tz="Etc/GMT+8"))
assert gaps.trim(data).all()
data.iloc[0:8] = False
data.iloc[9] = False
expected = data.copy()
expected.iloc[0:10] = False
assert_series_equal(
expected,
gaps.trim(data)
)
data.iloc[-5:] = False
expected.iloc[-5:] = False
assert_series_equal(
expected,
gaps.trim(data)
)


def test_completeness_score_all_nans():
"""A data set with all nans has completeness 0 for each day."""
completeness = gaps.completeness_score(
Expand Down

0 comments on commit 201b495

Please sign in to comment.