cfdm.Data.flatten

Data.flatten(axes=None, inplace=False)[source]

Flatten axes of the data.

Any subset of the axes may be flattened.

The shape of the data may change, but the size will not.

The flattening is executed in row-major (C-style) order. For example, the array [[1, 2], [3, 4]] would be flattened across both dimensions to [1 2 3 4].

New in version (cfdm): 1.7.11

Parameters
axes: (sequence of) int, optional

Select the axes. By default all axes are flattened. No axes are flattened if axes is an empty sequence.

Each axis is identified by its integer position in the data. Negative integers counting from the last position are allowed.

Parameter example:

axes=0

Parameter example:

axes=-1

Parameter example:

axes=[1, -2]

inplace: bool, optional

If True then do the operation in-place and return None.

Returns
Data or None

The flattened data, or None if the operation was in-place.

Examples

>>> d = cfdm.Data(numpy.arange(24).reshape(1, 2, 3, 4))
>>> d
<Data(1, 2, 3, 4): [[[[0, ..., 23]]]]>
>>> print(d.array)
[[[[ 0  1  2  3]
   [ 4  5  6  7]
   [ 8  9 10 11]]
  [[12 13 14 15]
   [16 17 18 19]
   [20 21 22 23]]]]
>>> e = d.flatten()
>>> e
<Data(24): [0, ..., 23]>
>>> print(e.array)
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
>>> e = d.flatten([])
>>> e
<Data(1, 2, 3, 4): [[[[0, ..., 23]]]]>
>>> e = d.flatten([1, 3])
>>> e
<Data(1, 8, 3): [[[0, ..., 23]]]>
>>> print(e.array)
[[[ 0  4  8]
  [ 1  5  9]
  [ 2  6 10]
  [ 3  7 11]
  [12 16 20]
  [13 17 21]
  [14 18 22]
  [15 19 23]]]
>>> d.flatten([0, -1], inplace=True)
>>> d
<Data(4, 2, 3): [[[0, ..., 23]]]>
>>> print(d.array)
[[[ 0  4  8]
  [12 16 20]]
 [[ 1  5  9]
  [13 17 21]]
 [[ 2  6 10]
  [14 18 22]]
 [[ 3  7 11]
  [15 19 23]]]