What does the pandas.series.array attribute do?

PandasServer Side ProgrammingProgramming

The “.array” is one of the pandas series attributes. it will return a pandas ExtensionArray with the values stored in the series. The “.array” is used to get a zero-copy reference to the underlying data.

The resultant array is not like a NumPy array it is an ExtensionArray, and it has different array types based on the data present in the series (dtype).

Example 1

import pandas as pd

# create pandas series with numerical values
s1 = pd.Series([1,2,3,4])

print(s1)

print(s1.array)

Explanation

The “s1” is the pandas series object which is created by using integer values with length 4. And we have applied the pandas array attribute to this series object s1.

Output

0    1
1    2
2    3
3    4
dtype: int64

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

The resultant ExtensionArray is created for the series object “s1” that can be seen in the above output block which is PandasArray.

Example 2

import pandas as pd

# creating dates
date = pd.date_range("2021-06-01", periods=5, freq="M")

# creating pandas Series with date sequence
s2 = pd.Series(date)

print(s2)

print(s2.array)

Explanation

Let’s take a series with date-time sequence values, and get an ExtensionArray using the series attribute “.array”.

Output

0    2021-06-30
1    2021-07-31
2    2021-08-31
3    2021-09-30
4    2021-10-31
dtype: datetime64[ns]

<DatetimeArray>
['2021-06-30 00:00:00', '2021-07-31 00:00:00', '2021-08-31 00:00:00',
'2021-09-30 00:00:00', '2021-10-31 00:00:00']
Length: 5, dtype: datetime64[ns]

In the following example, we can see a DatatimeArray created by using the “array” attribute. This DatatimeArray is a type of ExtensionArray which is for DateTime dtype data in pandas.

For any other 3rd-party dtype, the array type will be the ExtensionArray only.

raja
Updated on 09-Mar-2022 05:23:29

Advertisements