• Post author:
• Post category:Pandas

`pandas.Series()` function is used to convert the NumPy array to Pandas Series. Pandas Series and NumPy array have a similar feature in structure so, we can convert very easily from array to the series. To convert the array to Series we need to import both the NumPy module and Pandas module.

In this article, I will explain `pandas.series()` function using this how we can convert From NumPy array to Pandas Series. In case, if you want to convert Pandas Series to NumPy array use `Series.to_numpy()`.

Key Points –

• Use the `pd.Series()` constructor from Pandas to convert a NumPy array into a Pandas Series.
• Ensure that the data type of the NumPy array elements is preserved in the resulting Pandas Series, allowing for consistency and compatibility with downstream operations.
• Pandas automatically assigns numerical indices starting from 0 to the elements of the Series.
• The dtype of the NumPy array elements will be preserved in the resulting Pandas Series.

## 1. Quick Examples of Convert Array to Pandas Series

If you are in hurry below are some quick examples of the NumPy Array to Pandas Series conversion.

``````
# Quick examples of convert array to pandas series

# Example 1: Create a NumPy array
# Using np.zeros()
array = np.zeros(5)

# Convert the NumPy array
# To a Pandas series
series = pd.Series(array)

# Example 2: Create a NumPy array numpy.ones()
array = np.ones(5)

# Convert the NumPy array
# To a Pandas series
series = pd.Series(array)

# Example 3 : Create a NumPy array
# Using random.rand()
array = np.random.rand(5)

# Convert the NumPy array
# To a Pandas series
series = pd.Series(array)
``````

## 2. Syntax of pandas.Series()

Following is the syntax of the

``````
# Syntax of pandas.Series()
(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False)
``````

### 2.1 Parameters

• `dtype`: Data type which we are passing like str.
• `copy` : [bool, default False] Ensures that the returned value is a not a view on another array.

### 2.2 Return Value

It returns Pandas Series.

## 3. Convert NumPy Array to Pandas Series

NumPy array is a data structure (usually numbers), all of the same type, it looks the same as a list. But arrays are more efficient than Python lists and also much more compact. We can also convert Pandas DataFrame to a Numpy array.

Pandas Series is a one-dimensional array that is capable of storing various data types (integer, string, float, python objects, etc.). We can easily convert the Pandas Series to list, Series to the dictionary, and Series to tuple using the `Series()` method. Let’s create NumPy array using np.zeros() function then, convert array to Series using `pd.series()` function.

``````
import pandas as pd
import numpy as np

# Create a NumPy array using np.zeros()
array = np.zeros(5)
print("NumPy array:\n", array)

# Convert the NumPy array to a Pandas series
series = pd.Series(array)
print(" Pandas Series:\n", series)
print(type(series))
``````

Yields below output.

``````
# Output:
NumPy array:
[0. 0. 0. 0. 0.]

Pandas Series:
0    0.0
1    0.0
2    0.0
3    0.0
4    0.0
dtype: float64
``````

### 3.1 Use ones() Function Create Array & Convert Series.

Here, we can create Numpy array using np.ones() function then, convert array to Series using pandas `series()` function. It will return the Pandas Series.

``````
# Create a NumPy array numpy.ones()
array = np.ones(5)
print("NumPy array:\n", array)

# Convert the NumPy array to Pandas series
series = pd.Series(array)
print(" Pandas Series:\n", series)
print(type(series))
``````

Yields below output.

``````
# Output:

NumPy array:
[1. 1. 1. 1. 1.]

Pandas Series:
0    1.0
1    1.0
2    1.0
3    1.0
4    1.0
dtype: float64
``````

## 3.2 Use numpy. random.rand() Function Create Array & Convert Series.

Use numpy.random.rand() function create Numpy array then, convert array to Series using `pd.series()` function.

``````
# Create a NumPy array using random.rand()
array = np.random.rand(5)
print("NumPy array:\n", array)

# Convert the NumPy array
# To a Pandas series
series = pd.Series(array)
print(" Pandas Series:\n", series)
print(type(series))
``````

Yields below output.

``````
# Output:

NumPy array:
[0.13894623 0.96695242 0.41112637 0.19469214 0.21145509]

Pandas Series:
0    0.639158
1    0.272269
2    0.402008
3    0.462556
4    0.490533
dtype: float64
``````

## Frequently Asked Questions on Convert NumPy Array to Series

Why should I convert a NumPy array to a Pandas Series?

Converting a NumPy array to a Pandas Series allows you to leverage the extensive functionality and flexibility offered by Pandas. Series provide labeled indices, alignment, and a wide range of data manipulation capabilities not available in NumPy arrays.

Will converting a NumPy array to a Pandas Series change the underlying data?

Converting a NumPy array to a Pandas Series does not change the underlying data. It merely provides a different interface for accessing and manipulating the data.

What happens if I have missing or NaN values in my NumPy array?

Pandas Series can handle missing or NaN values, and they will be preserved during the conversion process. NaN values in NumPy arrays will be represented as NaN in the resulting Pandas Series.

Can I convert a multi-dimensional NumPy array to a Pandas Series?

Pandas Series are one-dimensional data structures. However, you can convert a multi-dimensional NumPy array into a Pandas DataFrame, which is a two-dimensional data structure, using the `pd.DataFrame()` constructor.

## Conclusion

In this article, I have explained `pandas.series()` function using this how we can convert from NumPy array to Pandas Series with multiple examples.