Series#
- class gwpy.types.Series(
- value: QuantityLike,
- unit: UnitBase | str | None = None,
- x0: QuantityLike | None = None,
- dx: QuantityLike | None = None,
- xindex: QuantityLike | None = None,
- xunit: UnitBase | str | None = None,
- **kwargs,
Bases:
ArrayA one-dimensional data series.
A
Seriesis defined as an array of data indexed upon an axis, meaning each sample maps to a position upon the axis. By convention the X axis is used to define the index, with thex0,dx, andxindexattributes allowing the positions of the data to be well defined.- Parameters:
- valuearray_like
Input data array.
- unit
Unit, optional Physical unit of these data.
- x0
float,Quantity, optional, default:0 The starting value for the x-axis of this array.
- dx
float,Quantity, optional, default: `1 The step size for the x-axis of this array.
- xindex
array-like The complete array of x-axis values for this array. This argument takes precedence over
x0anddxso should be given in place of these if relevant, not alongside.- xunit
Unit, optional The unit of the x-axis index. If not given explicitly, it will be taken from any of
dx,x0, orxindex, or set to a boring default.- epoch
LIGOTimeGPS,float,str, optional GPS epoch associated with these data, any input parsable by
to_gpsis fine.- name
str, optional Descriptive title for this array.
- channel
Channel,str, optional Source data stream for these data.
- dtype
dtype, optional Input data type.
- copy
bool, optional, default:False Choose to copy the input data to new memory.
- subok
bool, optional, default:True Allow passing of sub-classes by the array generator.
- Returns:
Examples
To define a
Seriesof displacements at a given input laser power, for example:>>> data = Series([1, 2, 3, 2, 4, 3], unit='nm', x0=0, dx=2, xunit='W') >>> print(data) Series([ 1., 2., 3., 2., 4., 3.] unit: Unit("nm"), name: None, epoch: None, channel: None, x0: 0.0 W, dx: 2.0 W, xindex: [ 0. 2. 4. 6. 8. 10.] W)
Attributes Summary
X-axis sample separation.
Read data into a
Series.Write this
Seriesto a file.X-axis coordinate of the first data point.
Positions of the data on the x-axis.
X-axis [low, high) segment encompassed by these data.
Unit of x-axis index.
Methods Summary
append(other, *[, inplace, gap, pad, resize])Connect another series onto this one.
check_compatible(other[, casting, ...])Check whether this Series and
otherare compatible.copy([order])Return a copy of the array.
crop([start, end, copy])Crop this series to the given x-axis extent.
diff([n, axis])Calculate the n-th order discrete difference along given axis.
inject(other, *[, inplace])Add two compatible
Seriesalong their shared x-axis values.is_compatible(other)Check whether this series and other have compatible metadata.
is_contiguous(other[, tol])Check whether other is contiguous with self.
pad(pad_width, **kwargs)Pad this series to a new size.
plot([method])Plot the data for this series.
prepend(other, *[, inplace, gap, pad, resize])Connect another series onto the start of the current one.
shift(delta)Shift this
Seriesforward on the X-axis bydelta.step(**kwargs)Create a step plot of this series.
update(other, *[, inplace, gap, pad])Update this series by appending new data like a buffer.
value_at(x)zip()Attributes Documentation
- read#
Read data into a
Series.Arguments and keywords depend on the output format, see the online documentation for full details for each format, the parameters below are common to most formats.
- Parameters:
- source
str,pathlib.Path,file,list Source of data, any of the following:
open (readable) file object
path to a file
list of files (open or paths)
- format
str, Source format identifier. If not given, the format will be detected if possible. See below for list of acceptable formats.
- kwargs
Other keywords arguments depend on the format, see the online documentation for details.
- source
- Returns:
- data
Series
- data
- Raises:
IndexErrorif
sourceis an empty list
- write#
Write this
Seriesto a file.Arguments and keywords depend on the output format, see the online documentation for full details for each format, the parameters below are common to most formats.
Methods Documentation
- append(
- other: numpy.ndarray | Series,
- *,
- inplace: bool = True,
- gap: Literal['raise', 'ignore', 'pad'] | None = None,
- pad: float | None = None,
- resize: bool = True,
Connect another series onto this one.
- Parameters:
- other
numpy.ndarray,Series Another
Series, or a simple data array to connect to this one.- inplace
bool, optional If
True(default) perform the operation in-place, modifying current series. IfFalsecopy the data to new memory before modifying.Warning
inplaceappend bypasses the reference check innumpy.ndarray.resize, so be carefully to only use this for arrays that haven’t been sharing their memory!- gap
str, optional Action to perform if there’s a gap between the other series and this one. One of
'raise'- raise aValueError'ignore'- remove gap and join data'pad'- pad gap with zeros
If
padis given and is notNone, the default isgap='pad', otherwisegap='raise'.If
gap='pad'is given, the default forpadis0.- pad
float, optional Value with which to pad discontiguous series, by default gaps will result in a
ValueError.- resize
bool, optional If
True(default) resize this array to accommodate new data. IfFalseroll the current data like a buffer to the left and insert new data at the other end.
- other
- Returns:
- series
Series A new series containing joined data sets.
- series
- check_compatible(
- other: list | numpy.ndarray,
- casting: Literal['no', 'equiv', 'safe', 'same_kind', 'unsafe'] | None = 'safe',
- *,
- irregular_equal: bool = True,
Check whether this Series and
otherare compatible.- Parameters:
- other
numpy.ndarray,Series The array to compare to.
- casting
str, optional The type of casting to support when comparing dtypes.
- irregular_equal
bool, optional Require irregular indices to be equal (default). If
irregular_equal=Falseand either (or both) of the series are irregular, this method just returns without doing anything.
- other
- Raises:
ValueErrorIf any metadata elements aren’t compatible.
TypeErrorIf the dtype can’t be safely cast between the arrays.
- copy(order='C')[source]#
Return a copy of the array.
- Parameters:
- order{‘C’, ‘F’, ‘A’, ‘K’}, optional
Controls the memory layout of the copy. ‘C’ means C-order, ‘F’ means F-order, ‘A’ means ‘F’ if
ais Fortran contiguous, ‘C’ otherwise. ‘K’ means match the layout ofaas closely as possible. (Note that this function andnumpy.copy()are very similar but have different default values for their order= arguments, and this function always passes sub-classes through.)
See also
numpy.copySimilar function with different default behavior
numpy.copyto
Notes
This function is the preferred method for creating an array copy. The function
numpy.copy()is similar, but it defaults to using order ‘K’, and will not pass sub-classes through by default.Examples
>>> import numpy as np >>> x = np.array([[1,2,3],[4,5,6]], order='F')
>>> y = x.copy()
>>> x.fill(0)
>>> x array([[0, 0, 0], [0, 0, 0]])
>>> y array([[1, 2, 3], [4, 5, 6]])
>>> y.flags['C_CONTIGUOUS'] True
For arrays containing Python objects (e.g. dtype=object), the copy is a shallow one. The new array will contain the same object which may lead to surprises if that object can be modified (is mutable):
>>> a = np.array([1, 'm', [2, 3, 4]], dtype=object) >>> b = a.copy() >>> b[2][0] = 10 >>> a array([1, 'm', list([10, 3, 4])], dtype=object)
To ensure all elements within an
objectarray are copied, usecopy.deepcopy:>>> import copy >>> a = np.array([1, 'm', [2, 3, 4]], dtype=object) >>> c = copy.deepcopy(a) >>> c[2][0] = 10 >>> c array([1, 'm', list([10, 3, 4])], dtype=object) >>> a array([1, 'm', list([2, 3, 4])], dtype=object)
- crop( ) Self[source]#
Crop this series to the given x-axis extent.
- Parameters:
- Returns:
- series
Series A new series with a sub-set of the input data.
- series
Notes
If either
startorendare outside of the originalSeriesspan, warnings will be printed and the limits will be restricted to thexspan.
- diff(n: int = 1, axis: int = -1) Self[source]#
Calculate the n-th order discrete difference along given axis.
The first order difference is given by
out[n] = a[n+1] - a[n]along the given axis, higher order differences are calculated by usingdiffrecursively.- Parameters:
- Returns:
- diff
Series The
norder differences. The shape of the output is the same as the input, except alongaxiswhere the dimension is smaller byn.
- diff
See also
numpy.diffFor documentation on the underlying method.
- inject(other: Series, *, inplace: bool = False) Self[source]#
Add two compatible
Seriesalong their shared x-axis values.- Parameters:
- Returns:
- out
Series The sum of
selfandotheralong their shared x-axis values.
- out
- Raises:
ValueErrorIf
selfandotherhave incompatible units or xindex intervals.
Notes
The offset between
selfandotherwill be rounded to the nearest sample if they are not exactly aligned.If
self.xindexis an array of timestamps, and ifother.xspanis not a subset ofself.xspan, thenotherwill be cropped before being adding toself.Users may wish to taper or window their
Seriesbefore passing it to this method. SeeTimeSeries.taper()andplanck()for more information.
- is_compatible(other: list | ndarray) bool[source]#
Check whether this series and other have compatible metadata.
This method tests that the
sample size, and theunitmatch.
- is_contiguous( ) int[source]#
Check whether other is contiguous with self.
- Parameters:
- other
Series,numpy.ndarray Another series of the same type to test for contiguity.
- tol
float, optional The numerical tolerance of the test.
- other
- Returns:
- 1
If
otheris contiguous with this series, i.e. would attach seamlessly onto the end.
- -1
If
otheris anti-contiguous with this seires, i.e. would attach seamlessly onto the start.
- 0
If
otheris completely dis-contiguous with this series.
Notes
If
otheris an array that doesn’t have index information (e.g. anumpy.ndarray), this method always returns1.If
self*or*other`have an irregularIndexarray (e.g. aren’t linearly sampled), this method will always return1ifotherstarts afterselffinishes, or-1`if the inverse. If the two arrays overlap, that is bad and will raise an error.
- pad(pad_width: int | tuple[int, int], **kwargs) Self[source]#
Pad this series to a new size.
This just wraps
numpy.padand handles shifting theIndexto accommodate padding on the left.- Parameters:
- Returns:
- series
Series The padded version of the input.
- series
See also
numpy.padFor details on the pad function and valid keyword arguments.
- plot(method: str = 'plot', **kwargs) Plot[source]#
Plot the data for this series.
- Parameters:
- Returns:
- figure
Figure The newly created figure, with populated Axes.
- figure
See also
matplotlib.pyplot.figureFor documentation of keyword arguments used to create the figure.
matplotlib.figure.Figure.add_subplotFor documentation of keyword arguments used to create the axes.
matplotlib.axes.Axes.plotFor documentation of keyword arguments used in rendering the data.
- prepend(
- other: QuantityLike,
- *,
- inplace: bool = True,
- gap: Literal['raise', 'ignore', 'pad'] | None = None,
- pad: float | None = None,
- resize: bool = True,
Connect another series onto the start of the current one.
- Parameters:
- other
numpy.ndarray,Series The data to prepend to this series.
- inplace
bool, optional If
True(default) perform the operation in-place, modifying current series. IfFalsecopy the data to new memory before modifying.Warning
inplaceappend bypasses the reference check innumpy.ndarray.resize, so be carefully to only use this for arrays that haven’t been sharing their memory!- gap
str, optional Action to perform if there’s a gap between the other series and this one. One of
'raise'- raise aValueError'ignore'- remove gap and join data'pad'- pad gap with zeros
If
padis given and is notNone, the default isgap='pad', otherwisegap='raise'.If
gap='pad'is given, the default forpadis0.- pad
float, optional Value with which to pad discontiguous series, by default gaps will result in a
ValueError.- resize
bool, optional If
True(default) resize this array to accommodate new data. IfFalseroll the current data like a buffer to the left or right (depending onprepend) and insert new data at the other end.
- other
- Returns:
- series
Series The modified series.
- series
- shift(delta: QuantityLike) None[source]#
Shift this
Seriesforward on the X-axis bydelta.This modifies the series in-place.
- Parameters:
Examples
>>> from gwpy.types import Series >>> a = Series([1, 2, 3, 4, 5], x0=0, dx=1, xunit='m') >>> print(a.x0) 0.0 m >>> a.shift(5) >>> print(a.x0) 5.0 m >>> a.shift('-1 km') -995.0 m
- step(**kwargs) Plot[source]#
Create a step plot of this series.
- kwargs
All keyword arguments are passed to the
plot()method. of this series.
See also
plotFor details of the plotting.
- update(
- other: QuantityLike,
- *,
- inplace: bool = True,
- gap: Literal['raise', 'ignore', 'pad'] | None = None,
- pad: float | None = None,
Update this series by appending new data like a buffer.
Old data (at the start) are dropped to maintain a fixed size.
This is a convenience method that just calls
appendwithresize=False.- Parameters:
- other
Series,numpy.ndarray The data to add to the end of this
Series.- inplace
bool If
True(default) modify the data in place. IfFalsecopy the data to new memory.- gap
str, optional Action to perform if there’s a gap between the other series and this one. One of
'raise'- raise aValueError'ignore'- remove gap and join data'pad'- pad gap with zeros
If
padis given and is notNone, the default isgap='pad', otherwisegap='raise'.If
gap='pad'is given, the default forpadis0.- pad
float, optional Value with which to pad discontiguous series, by default gaps will result in a
ValueError.
- other
- Returns:
- series
Series Either the same series (if
inplace=True) or a new series (ifinplace=False) withotherdata added to the end of this ‘buffer’.
- series
See also
appendFor details of the data manipulation.
- zip() ndarray[source]#
Zip the
xindexandvaluearrays of thisSeries.- Returns:
- stacked2-d
numpy.ndarray The array formed by stacking the the
xindexandvalueof this series
- stacked2-d
Examples
>>> a = Series([0, 2, 4, 6, 8], xindex=[-5, -4, -3, -2, -1]) >>> a.zip() array([[-5., 0.], [-4., 2.], [-3., 4.], [-2., 6.], [-1., 8.]])