larray.Array.reindex
- Array.reindex(axes_to_reindex=None, new_axis=None, fill_value=nan, inplace=False, **kwargs) Array [source]
Reorder and/or add new labels in axes.
Place NaN or given fill_value in locations having no value previously.
- Parameters
- axes_to_reindexaxis ref or dict {axis ref: axis} or list of (axis ref, axis) or sequence of Axis
Axis(es) to reindex. If a single axis reference is given, the new_axis argument must be provided. If string, Group or Axis object, the corresponding axis is reindexed if found among existing, otherwise a new axis is added. If a list of Axis or an AxisCollection is given, existing axes are reindexed while missing ones are added.
- new_axisint, str, list/tuple/array of str, Group or Axis, optional
List of new labels or new axis if axes_to_reindex contains a single axis reference.
- fill_valuescalar or Array, optional
Value used to fill cells corresponding to label combinations which were not present before reindexing. Defaults to NaN.
- inplacebool, optional
Whether to modify the original object or return a new array and leave the original intact. Defaults to False.
- **kwargsAxis
New axis for each axis to reindex given as a keyword argument.
- Returns
- Array
Array with reindexed axes.
Notes
When introducing NaNs into an array containing integers via reindex, all data will be promoted to float in order to store the NaNs.
Examples
>>> arr = ndtest((2, 2)) >>> arr a\b b0 b1 a0 0 1 a1 2 3 >>> arr2 = ndtest('a=a1,a2;c=c0;b=b2..b0') >>> arr2 a c\b b2 b1 b0 a1 c0 0 1 2 a2 c0 3 4 5
Reindex an axis by passing labels (list or string)
>>> arr.reindex('b', ['b1', 'b2', 'b0']) a\b b1 b2 b0 a0 1.0 nan 0.0 a1 3.0 nan 2.0 >>> arr.reindex('b', 'b0..b2', fill_value=-1) a\b b0 b1 b2 a0 0 1 -1 a1 2 3 -1 >>> arr.reindex(b='b=b0..b2', fill_value=-1) a\b b0 b1 b2 a0 0 1 -1 a1 2 3 -1
Reindex using an axis from another array
>>> arr.reindex('b', arr2.b, fill_value=-1) a\b b2 b1 b0 a0 -1 1 0 a1 -1 3 2
Reindex using a subset of an axis
>>> arr.reindex('b', arr2.b['b1':], fill_value=-1) a\b b1 b0 a0 1 0 a1 3 2
Reindex by passing an axis or a group
>>> arr.reindex('b=b2..b0', fill_value=-1) a\b b2 b1 b0 a0 -1 1 0 a1 -1 3 2 >>> arr.reindex(arr2.b, fill_value=-1) a\b b2 b1 b0 a0 -1 1 0 a1 -1 3 2 >>> arr.reindex(arr2.b['b1':], fill_value=-1) a\b b1 b0 a0 1 0 a1 3 2
Reindex several axes
>>> arr.reindex({'a': arr2.a, 'b': arr2.b}, fill_value=-1) a\b b2 b1 b0 a1 -1 3 2 a2 -1 -1 -1 >>> arr.reindex({'a': arr2.a, 'b': arr2.b['b1':]}, fill_value=-1) a\b b1 b0 a1 3 2 a2 -1 -1 >>> arr.reindex(a=arr2.a, b=arr2.b, fill_value=-1) a\b b2 b1 b0 a1 -1 3 2 a2 -1 -1 -1
Reindex by passing a collection of axes
>>> arr.reindex(arr2.axes, fill_value=-1) a b\c c0 a1 b2 -1 a1 b1 3 a1 b0 2 a2 b2 -1 a2 b1 -1 a2 b0 -1 >>> arr2.reindex(arr.axes, fill_value=-1) a c\b b0 b1 a0 c0 -1 -1 a1 c0 2 1