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

Let .sel take tolerance for datetime or str time #349

Merged
merged 1 commit into from
Mar 2, 2023
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
82 changes: 74 additions & 8 deletions pyleoclim/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2396,7 +2396,8 @@ def sel(self, value=None, time=None, tolerance=0):
`self.time` is between slice.start and slice.stop.
If slice of `datetime` (or str containing datetime, such as `'2020-01-01'`),
then the Series will be sliced so that `self.datetime_index` is
between `time.start` and `time.stop`.
between `time.start` and `time.stop` (+/- `tolerance`, which needs to be
a `timedelta`).
tolerance : int, float, default 0.
Used by `value` and `time`, see above.

Expand Down Expand Up @@ -2483,6 +2484,30 @@ def sel(self, value=None, time=None, tolerance=0):
if time is not None:
if isinstance(time, (int, float)):
return self.slice([time-tolerance, time+tolerance])
if isinstance(time, dt.datetime):
if tolerance == 0:
tolerance = dt.timedelta(days=0)
if not isinstance(tolerance, dt.timedelta):
raise TypeError(
f"Invalid 'tolerance' passed. Expected timedelta, received: {type(tolerance)}"
)
return self.pandas_method(
lambda x: x[(x.index>=time-tolerance) & (x.index<=time+tolerance)]
)
if isinstance(time, str):
if tolerance == 0:
tolerance = dt.timedelta(days=0)
if not isinstance(tolerance, dt.timedelta):
raise TypeError(
f"Invalid 'tolerance' passed. Expected timedelta, received: {type(tolerance)}"
)
tolerance = np.timedelta64(tolerance, 's')
return self.pandas_method(
lambda x: x[
(x.index>=np.datetime64(time, 's')-tolerance)
& (x.index<=np.datetime64(time, 's')+tolerance)
]
)
if isinstance(time, slice):
if isinstance(time.start, (int, float)) and isinstance(time.stop, (int, float)):
return self.slice([time.start-tolerance, time.stop+tolerance])
Expand All @@ -2493,36 +2518,77 @@ def sel(self, value=None, time=None, tolerance=0):
new.value = new.value[mask]
return new
if isinstance(time.stop, (int, float)) and time.start is None:
mask = self.time <= time.stop-tolerance
mask = self.time <= time.stop+tolerance
new = self.copy()
new.time = new.time[mask]
new.value = new.value[mask]
return new
if isinstance(time.start, str) and isinstance(time.stop, str):
if tolerance == 0:
tolerance = dt.timedelta(days=0)
if not isinstance(tolerance, dt.timedelta):
raise TypeError(
f"Invalid 'tolerance' passed. Expected timedelta, received: {type(tolerance)}"
)
tolerance = np.timedelta64(tolerance, 's')
return self.pandas_method(
lambda x: x[(x.index>=(np.datetime64(time.start, 's'))) & (x.index<=np.datetime64(time.stop, 's'))]
lambda x: x[
(x.index>=np.datetime64(time.start, 's')-tolerance)
& (x.index<=np.datetime64(time.stop, 's')+tolerance)
]
)
if isinstance(time.start, str) and time.stop is None:
if tolerance == 0:
tolerance = dt.timedelta(days=0)
if not isinstance(tolerance, dt.timedelta):
raise TypeError(
f"Invalid 'tolerance' passed. Expected timedelta, received: {type(tolerance)}"
)
tolerance = np.timedelta64(tolerance, 's')
return self.pandas_method(
lambda x: x[x.index>=(np.datetime64(time.start, 's'))]
lambda x: x[x.index>=np.datetime64(time.start, 's')-tolerance]
)
if isinstance(time.stop, str) and time.start is None:
if tolerance == 0:
tolerance = dt.timedelta(days=0)
if not isinstance(tolerance, dt.timedelta):
raise TypeError(f"Invalid 'tolerance' passed. Expected timedelta, received: {type(tolerance)}")
tolerance = np.timedelta64(tolerance, 's')
return self.pandas_method(
lambda x: x[x.index<=(np.datetime64(time.stop, 's'))]
lambda x: x[x.index<=np.datetime64(time.stop, 's')+tolerance]
)
if isinstance(time.start, dt.datetime) and isinstance(time.stop, dt.datetime):
if tolerance == 0:
tolerance = dt.timedelta(days=0)
if not isinstance(tolerance, dt.timedelta):
raise TypeError(
f"Invalid 'tolerance' passed. Expected timedelta, received: {type(tolerance)}"
)
return self.pandas_method(
lambda x: x[(x.index>=time.start) & (x.index<=time.stop)]
lambda x: x[(x.index>=time.start-tolerance) & (x.index<=time.stop+tolerance)]
)
if isinstance(time.start, dt.datetime) and time.stop is None:
if tolerance == 0:
tolerance = dt.timedelta(days=0)
if not isinstance(tolerance, dt.timedelta):
raise TypeError(
f"Invalid 'tolerance' passed. Expected timedelta, received: {type(tolerance)}"
)
return self.pandas_method(
lambda x: x[x.index>=time.start]
lambda x: x[x.index>=time.start-tolerance]
)
if isinstance(time.stop, dt.datetime) and time.start is None:
if tolerance == 0:
tolerance = dt.timedelta(days=0)
if not isinstance(tolerance, dt.timedelta):
raise TypeError(
f"Invalid 'tolerance' passed. Expected timedelta, received: {type(tolerance)}"
)
return self.pandas_method(
lambda x: x[x.index<=time.stop]
lambda x: x[x.index<=time.stop+tolerance]
)
raise TypeError("Expected int or float, or slice of int/float/datetime/str.")
raise TypeError("Invalid combination of arguments received.")


def slice(self, timespan):
Expand Down
8 changes: 8 additions & 0 deletions pyleoclim/tests/test_core_Series.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,8 @@ def test_value(self, value, expected_time, expected_value, tolerance):
[
(1, np.array([1]), np.array([4]), 0),
(1, np.array([1, 2]), np.array([4, 6]), 1),
(dt.datetime(1948, 1, 1), np.array([2, 3]), np.array([6, 1]), dt.timedelta(days=365)),
('1948', np.array([2, 3]), np.array([6, 1]), dt.timedelta(days=365)),
(slice(1, 2), np.array([1, 2]), np.array([4, 6]), 0),
(slice(1, 2), np.array([1, 2, 3]), np.array([4, 6, 1]), 1),
(slice(1, None), np.array([1, 2, 3]), np.array([4, 6, 1]), 0),
Expand All @@ -478,6 +480,12 @@ def test_value(self, value, expected_time, expected_value, tolerance):
(slice(dt.datetime(1948, 1, 1), dt.datetime(1949, 1, 1)), np.array([1, 2]), np.array([4, 6]), 0),
(slice(dt.datetime(1947, 1, 1), None), np.array([1, 2, 3]), np.array([4, 6, 1]), 0),
(slice(None, dt.datetime(1948, 1, 1)), np.array([3]), np.array([1]), 0),
(slice(dt.datetime(1948, 1, 1), dt.datetime(1949, 1, 1)), np.array([1, 2, 3]), np.array([4, 6, 1]), dt.timedelta(days=365)),
(slice(dt.datetime(1947, 1, 1), None), np.array([1, 2, 3]), np.array([4, 6, 1]), dt.timedelta(days=365)),
(slice(None, dt.datetime(1948, 1, 1)), np.array([2, 3]), np.array([6, 1]), dt.timedelta(days=365)),
(slice('1948', '1949'), np.array([1, 2, 3]), np.array([4, 6, 1]), dt.timedelta(days=365)),
(slice('1947', None), np.array([1, 2, 3]), np.array([4, 6, 1]), dt.timedelta(days=365)),
(slice(None, '1948'), np.array([2, 3]), np.array([6, 1]), dt.timedelta(days=365)),
]
)
def test_time(self, time, expected_time, expected_value, tolerance):
Expand Down