ShapedLikeNDArray

class astropy.utils.misc.ShapedLikeNDArray[source]

Bases: object

Mixin class to provide shape-changing methods.

The class proper is assumed to have some underlying data, which are arrays or array-like structures. It must define a shape property, which gives the shape of those data, as well as an _apply method that creates a new instance in which a ndarray method has been applied to those.

Furthermore, for consistency with ndarray, it is recommended to define a setter for the shape property, which, like the shape property allows in-place reshaping the internal data (and, unlike the reshape method raises an exception if this is not possible).

This class also defines default implementations for ndim and size properties, calculating those from the shape. These can be overridden by subclasses if there are faster ways to obtain those numbers.

Attributes Summary

T

Return an instance with the data transposed.

isscalar

ndim

The number of dimensions of the instance and underlying arrays.

shape

The shape of the underlying data.

size

The size of the object, as calculated from its shape.

Methods Summary

copy(self, \*args, \*\*kwargs)

Return an instance containing copies of the internal data.

diagonal(self, \*args, \*\*kwargs)

Return an instance with the specified diagonals.

flatten(self, \*args, \*\*kwargs)

Return a copy with the array collapsed into one dimension.

ravel(self, \*args, \*\*kwargs)

Return an instance with the array collapsed into one dimension.

reshape(self, \*args, \*\*kwargs)

Returns an instance containing the same data with a new shape.

squeeze(self, \*args, \*\*kwargs)

Return an instance with single-dimensional shape entries removed

swapaxes(self, \*args, \*\*kwargs)

Return an instance with the given axes interchanged.

take(self, indices[, axis, mode])

Return a new instance formed from the elements at the given indices.

transpose(self, \*args, \*\*kwargs)

Return an instance with the data transposed.

Attributes Documentation

T

Return an instance with the data transposed.

Parameters are as for T. All internal data are views of the data of the original.

isscalar
ndim

The number of dimensions of the instance and underlying arrays.

shape

The shape of the underlying data.

size

The size of the object, as calculated from its shape.

Methods Documentation

copy(self, *args, **kwargs)[source]

Return an instance containing copies of the internal data.

Parameters are as for copy().

diagonal(self, *args, **kwargs)[source]

Return an instance with the specified diagonals.

Parameters are as for diagonal(). All internal data are views of the data of the original.

flatten(self, *args, **kwargs)[source]

Return a copy with the array collapsed into one dimension.

Parameters are as for flatten().

ravel(self, *args, **kwargs)[source]

Return an instance with the array collapsed into one dimension.

Parameters are as for ravel(). Note that it is not always possible to unravel an array without copying the data. If you want an error to be raise if the data is copied, you should should assign shape (-1,) to the shape attribute.

reshape(self, *args, **kwargs)[source]

Returns an instance containing the same data with a new shape.

Parameters are as for reshape(). Note that it is not always possible to change the shape of an array without copying the data (see reshape() documentation). If you want an error to be raise if the data is copied, you should assign the new shape to the shape attribute (note: this may not be implemented for all classes using ShapedLikeNDArray).

squeeze(self, *args, **kwargs)[source]

Return an instance with single-dimensional shape entries removed

Parameters are as for squeeze(). All internal data are views of the data of the original.

swapaxes(self, *args, **kwargs)[source]

Return an instance with the given axes interchanged.

Parameters are as for swapaxes(): axis1, axis2. All internal data are views of the data of the original.

take(self, indices, axis=None, mode='raise')[source]

Return a new instance formed from the elements at the given indices.

Parameters are as for take(), except that, obviously, no output array can be given.

transpose(self, *args, **kwargs)[source]

Return an instance with the data transposed.

Parameters are as for transpose(). All internal data are views of the data of the original.