Skip to content
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

Update events for pandas v3.0 compatibility #247

Merged
merged 5 commits into from
Feb 20, 2024
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
4 changes: 2 additions & 2 deletions python/events/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ observations = observations.drop_duplicates(
# Resample to hourly, keep first measurement in each 1-hour bin
observations = observations.groupby([
'usgs_site_code',
Grouper(key='value_date', freq='H')
Grouper(key='value_date', freq='h')
]).first().ffill()

# Detect events
events = observations['value'].groupby(
level='usgs_site_code').apply(
list_events_helper,
level='usgs_site_code',
halflife='6H',
halflife='6h',
window='7D'
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.1.5"
__version__ = "1.1.6"
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ def event_boundaries(event_points: pd.Series):

"""
# Identify event starts
forward_shift = event_points.shift(1).fillna(False)
forward_shift = event_points.shift(1).astype(bool).fillna(False)
starts = (event_points & ~forward_shift)
starts = starts[starts]

# Identify event ends
backward_shift = event_points.shift(-1).fillna(False)
backward_shift = event_points.shift(-1).astype(bool).fillna(False)
ends = (event_points & ~backward_shift)
ends = ends[ends]

Expand Down Expand Up @@ -212,8 +212,8 @@ def mark_event_flows(
series: pd.Series,
halflife: Union[float, str, pd.Timedelta],
window: Union[int, pd.tseries.offsets.DateOffset, pd.Index],
minimum_event_duration: Union[pd.Timedelta, datetime.timedelta, np.timedelta64, str, int] = '0H',
start_radius: Union[pd.Timedelta, datetime.timedelta, np.timedelta64, str, int] = '0H'
minimum_event_duration: Union[pd.Timedelta, datetime.timedelta, np.timedelta64, str, int] = '0h',
start_radius: Union[pd.Timedelta, datetime.timedelta, np.timedelta64, str, int] = '0h'
) -> pd.Series:
"""Model the trend in a streamflow time series by taking the max
of two rolling minimum filters applied in a forward and
Expand All @@ -236,10 +236,10 @@ def mark_event_flows(
window: int, offset, or BaseIndexer subclass, required
Size of the moving window for `pandas.Series.rolling.min`.
This filter is used to model the trend in `series`.
minimum_event_duration: pandas.Timedelta, datetime.timedelta, numpy.timedelta64, str, int, optional, default '0H'
minimum_event_duration: pandas.Timedelta, datetime.timedelta, numpy.timedelta64, str, int, optional, default '0h'
Enforce a minimum event duration. This should generally be set equal to
halflife to reduce the number of false positives flagged as events.
start_radius: pandas.Timedelta, datetime.timedelta, numpy.timedelta64, str, int, optional, default '0H'
start_radius: pandas.Timedelta, datetime.timedelta, numpy.timedelta64, str, int, optional, default '0h'
Shift event starts to a local minimum. Phase shifts imparted on the
original signal may advance or delay event start times depending upon how
much smoothing is required to eliminate noise.
Expand Down Expand Up @@ -293,8 +293,8 @@ def list_events(
series: pd.Series,
halflife: Union[float, str, pd.Timedelta],
window: Union[int, pd.tseries.offsets.DateOffset, pd.Index],
minimum_event_duration: Union[pd.Timedelta, datetime.timedelta, np.timedelta64, str, int] = '0H',
start_radius: Union[pd.Timedelta, datetime.timedelta, np.timedelta64, str, int] = '0H'
minimum_event_duration: Union[pd.Timedelta, datetime.timedelta, np.timedelta64, str, int] = '0h',
start_radius: Union[pd.Timedelta, datetime.timedelta, np.timedelta64, str, int] = '0h'
) -> pd.DataFrame:
"""Apply time series decomposition to mark event values in a streamflow
time series. Discretize continuous event values into indiviual events.
Expand All @@ -312,10 +312,10 @@ def list_events(
window: int, offset, or BaseIndexer subclass, required
Size of the moving window for `pandas.Series.rolling.min`.
This filter is used to model the trend in `series`.
minimum_event_duration: pandas.Timedelta, datetime.timedelta, numpy.timedelta64, str, int, optional, default '0H'
minimum_event_duration: pandas.Timedelta, datetime.timedelta, numpy.timedelta64, str, int, optional, default '0h'
Enforce a minimum event duration. This should generally be set equal to
halflife to reduce the number of false positives flagged as events.
start_radius: pandas.Timedelta, datetime.timedelta, numpy.timedelta64, str, int, optional, default '0H'
start_radius: pandas.Timedelta, datetime.timedelta, numpy.timedelta64, str, int, optional, default '0h'
Shift event starts to a local minimum. Phase shifts imparted on the
original signal may advance or delay event start times depending upon how
much smoothing is required to eliminate noise.
Expand Down
22 changes: 11 additions & 11 deletions python/events/tests/test_decomposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ def test_list_events():
index=pd.date_range(
start=pd.to_datetime('2018-01-01'),
periods=len(t),
freq='H'),
freq='h'),
name='streamflow'
)

# Detect event
events = ev.list_events(series, '6H', '7D')
events = ev.list_events(series, '6h', '7D')

# Should detect a single event
assert len(events.index) == 1
Expand Down Expand Up @@ -56,12 +56,12 @@ def test_list_events_noise():
index=pd.date_range(
start=pd.to_datetime('2018-01-01'),
periods=len(t),
freq='H'),
freq='h'),
name='streamflow'
)

# Detect event
events = ev.list_events(series, '6H', '7D', '6H', '7H')
events = ev.list_events(series, '6h', '7D', '6h', '7h')

# Should detect a single event
assert len(events.index) == 1
Expand All @@ -80,7 +80,7 @@ def test_local_minimum_datetime_exception():
with pytest.raises(Exception):
idx = ev.find_local_minimum(
pd.Timestamp('2020-01-01 01:00'),
'3H',
'3h',
series
)

Expand All @@ -91,15 +91,15 @@ def test_origin_not_idx():
index=pd.date_range(
start=pd.to_datetime('2018-01-01'),
periods=10,
freq='H'),
freq='h'),
name='streamflow'
)

# Test local minimum
with pytest.raises(Exception):
idx = ev.find_local_minimum(
pd.Timestamp('2020-01-01 01:00'),
'3H',
'3h',
series
)

Expand All @@ -110,7 +110,7 @@ def test_bad_time_series_idx():
index=[i for i in range(5)]
)
with pytest.raises(Exception):
events = ev.list_events(series, '6H', '7D')
events = ev.list_events(series, '6h', '7D')

# Not monotonic
series = pd.Series(
Expand All @@ -125,7 +125,7 @@ def test_bad_time_series_idx():
name='streamflow'
)
with pytest.raises(Exception):
events = ev.list_events(series, '6H', '7D')
events = ev.list_events(series, '6h', '7D')

# Duplicated
series = pd.Series(
Expand All @@ -140,7 +140,7 @@ def test_bad_time_series_idx():
name='streamflow'
)
with pytest.raises(Exception):
events = ev.list_events(series, '6H', '7D')
events = ev.list_events(series, '6h', '7D')

def test_null_warning():
series = pd.Series(
Expand All @@ -155,4 +155,4 @@ def test_null_warning():
name='streamflow'
)
with pytest.warns(UserWarning):
events = ev.list_events(series, '6H', '7D')
events = ev.list_events(series, '6h', '7D')
Loading