clouddrift.ragged.ragged_to_regular

clouddrift.ragged.ragged_to_regular#

clouddrift.ragged.ragged_to_regular(ragged: ndarray | Series | DataArray, rowsize: list | ndarray | Series | DataArray, fill_value: float = nan) ndarray[source]#

Convert a ragged array to a two-dimensional array such that each contiguous segment of a ragged array is a row in the two-dimensional array. Each row of the two-dimensional array is padded with NaNs as needed. The length of the first dimension of the output array is the length of rowsize. The length of the second dimension is the maximum element of rowsize.

Note: Although this function accepts parameters of type xarray.DataArray, passing NumPy arrays is recommended for performance reasons.

Parameters#

raggednp.ndarray or pd.Series or xr.DataArray

A ragged array.

rowsizelist or np.ndarray[int] or pd.Series or xr.DataArray[int]

The size of each row in the ragged array.

fill_valuefloat, optional

Fill value to use for the trailing elements of each row of the resulting regular array.

Returns#

np.ndarray

A two-dimensional array.

Examples#

By default, the fill value used is NaN:

>>> ragged_to_regular(np.array([1, 2, 3, 4, 5]), np.array([2, 1, 2]))
array([[ 1.,  2.],
       [ 3., nan],
       [ 4.,  5.]])

You can specify an alternative fill value:

>>> ragged_to_regular(np.array([1, 2, 3, 4, 5]), np.array([2, 1, 2]), fill_value=999)
array([[  1,   2],
       [  3, 999],
       [  4,   5]])

See Also#

regular_to_ragged()