Open In App

pandas.array() function in Python

Last Updated : 14 Aug, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

This method is used to create an array from a sequence in desired data type.

Syntax : pandas.array(data: Sequence[object], dtype: Union[str, numpy.dtype, pandas.core.dtypes.base.ExtensionDtype, NoneType] = None, copy: bool = True)

Parameters :

  • data : Sequence of objects. The scalars inside `data` should be instances of the scalar type for `dtype`. It's expected that `data` represents a 1-dimensional array of data. When `data` is an Index or Series, the underlying array will be extracted from `data`.
  • dtype : tr, np.dtype, or ExtensionDtype, optional. The dtype to use for the array. This may be a NumPy dtype or an extension type registered with pandas.
  • copy : bool, default True. Whether to copy the data, even if not necessary. Depending on the type of `data`, creating the new array may require copying data, even if ``copy=False``.

Below is the implementation of the above method with some examples :

Example 1 :

Python3
# importing packages
import pandas

# create Pandas array with dtype string
pd_arr = pandas.array(data=[1,2,3,4,5],dtype=str)

# print the formed array
print(pd_arr)

Output :

<PandasArray>
['1', '2', '3', '4', '5']
Length: 5, dtype: str32

Example 2 :

Python3
# importing packages
import pandas
import numpy

# create Pandas array with dtype from numpy
pd_arr = pandas.array(data=['1', '2', '3', '4', '5'],
                      dtype=numpy.int8)

# print the formed array
print(pd_arr)

Output :

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

Practice Tags :

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy