larray.AxisCollection.replace

AxisCollection.replace(axes_to_replace=None, new_axis=None, inplace=False, **kwargs) AxisCollection[source]

Replace one, several or all axes of the collection.

Parameters
axes_to_replaceaxis ref or dict {axis ref: axis} or list of (tuple or Axis) or AxisCollection, optional

Axes to replace. If a single axis reference is given, the new_axis argument must be provided. If a list of Axis or an AxisCollection is given, all axes will be replaced by the new ones. In that case, the number of new axes must match the number of the old ones. Defaults to None. If a list of tuple is given, it must be pairs of (reference to old axis, new axis).

new_axisaxis ref, optional

New axis if axes_to_replace contains a single axis reference. Defaults to None.

inplacebool, optional

Whether to modify the original object or return a new AxisCollection and leave the original intact. Defaults to False.

**kwargsAxis

New axis for each axis to replace given as a keyword argument.

Returns
AxisCollection

AxisCollection with axes replaced.

Examples

>>> from larray import ndtest
>>> axes = ndtest((2, 3)).axes
>>> axes
AxisCollection([
    Axis(['a0', 'a1'], 'a'),
    Axis(['b0', 'b1', 'b2'], 'b')
])
>>> row = Axis(['r0', 'r1'], 'row')
>>> column = Axis(['c0', 'c1', 'c2'], 'column')

Replace one axis (second argument new_axis must be provided)

>>> axes.replace(X.a, row)  
>>> # or
>>> axes.replace(X.a, "row=r0,r1")
AxisCollection([
    Axis(['r0', 'r1'], 'row'),
    Axis(['b0', 'b1', 'b2'], 'b')
])

Replace several axes (keywords, list of tuple or dictionary)

>>> axes.replace(a=row, b=column)  
>>> # or
>>> axes.replace(a="row=r0,r1", b="column=c0,c1,c2")  
>>> # or
>>> axes.replace([(X.a, row), (X.b, column)])  
>>> # or
>>> axes.replace({X.a: row, X.b: column})
AxisCollection([
    Axis(['r0', 'r1'], 'row'),
    Axis(['c0', 'c1', 'c2'], 'column')
])

Replace all axes (list of axes or AxisCollection)

>>> axes.replace([row, column])
AxisCollection([
    Axis(['r0', 'r1'], 'row'),
    Axis(['c0', 'c1', 'c2'], 'column')
])
>>> arr = ndtest([row, column])
>>> axes.replace(arr.axes)
AxisCollection([
    Axis(['r0', 'r1'], 'row'),
    Axis(['c0', 'c1', 'c2'], 'column')
])