pynapple.core.time_series.Tsd.get_slice#

Tsd.get_slice(start, end=None, time_unit='s')#

Get a slice object from the time series data based on the start and end values such that all the timestamps satisfy start<=t<=end. If end is None, only the timepoint closest to start is returned.

By default, the time support doesn’t change. If you want to change the time support, use the restrict function.

This function is equivalent of calling the get method.

Parameters:
  • start (int or float) – The starting value for the slice.

  • end (int or float, optional) – The ending value for the slice. Defaults to None.

  • time_unit (str, optional) – The time unit for the start and end values. Defaults to “s” (seconds).

Returns:

slice – A slice determining the start and end indices, with unit step Slicing the array will be equivalent to calling get: ts[s].t == ts.get(start, end).t with s being the slice object.

Return type:

slice

Raises:

ValueError

  • If start or end is not a number. - If start is greater than end.

Examples

>>> import pynapple as nap
>>> ts = nap.Ts(t = [0, 1, 2, 3])
>>> # slice over a range
>>> start, end = 1.2, 2.6
>>> print(ts.get_slice(start, end))  # returns `slice(2, 3, None)`
>>> start, end = 1., 2.
>>> print(ts.get_slice(start, end, mode="forward"))  # returns `slice(1, 3, None)`
>>> # slice a single value
>>> start = 1.2
>>> print(ts.get_slice(start))  # returns `slice(1, 2, None)`
>>> start = 2.
>>> print(ts.get_slice(start)) # returns `slice(2, 3, None)`