w3resource

Pandas Series property: array

An ExtensionArray in Pandas

The ExtensionArray of the data backing Pandas Series or Index.

Syntax:

Series.array
Pandas Series array property.

Returns: ExtensionArray - An ExtensionArray of the values stored within.
For extension types, this is the actual array.
For NumPy native types, this is a thin (no copy) wrapper around numpy.ndarray.

Following table lays out the different array types for each extension dtype within pandas.

dtype array type
category

Categorical

period PeriodArray
interval IntervalArray
IntegerNA IntegerArray
datetime64[ns, tz] DatetimeArray

For any 3rd-party extension types, the array type will be an ExtensionArray.
For all remaining dtypes .array will be a arrays.NumpyExtensionArray wrapping the actual ndarray stored within. If you absolutely need a NumPy array (possibly with copying / coercing data), then use Series.to_numpy() instead.

Example:

For regular NumPy types like int, and float, a PandasArray is returned.

Python-Pandas Code:

import numpy as np
import pandas as pd
pd.Series([2, 3, 4]).array

Output:

<PandasArray>
[2, 3, 4]
Length: 3, dtype: int64

Example - For extension types, like Categorical, the actual ExtensionArray is returned:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(pd.Categorical(['x', 'y', 'x']))
s.array

Output:

[x, y, x]
Categories (2, object): [x, y]
Pandas: Series array.

Previous: Series
Next: Series as ndarray or ndarray-like in Pandas



Follow us on Facebook and Twitter for latest update.