w3resource

Pandas Series: to_numpy() function

Values in Pandas Series or Index

A NumPy ndarray representing the values in this Series or Index.

Syntax:

Series.to_numpy(self, dtype=None, copy=False)
Pandas Series to_numpy() function

Parameters:

Name Description Type/Default Value Required / Optional
dtype The dtype to pass to numpy.asarray()
str or numpy.dtype Optional
copy Whether to ensure that the returned value is a not a view on another array. Note that copy=False does not ensure that to_numpy() is no-copy. Rather, copy=True ensure that a copy is made, even if not strictly necessary. bool
Default Value: False
Required

Returns: numpy.ndarray

Notes:

The returned array will be the same up to equality (values equal in self will be equal in the returned array; likewise for values that are not equal). When self contains an ExtensionArray, the dtype may be different. For example, for a category-dtype Series, to_numpy() will return a NumPy array and the categorical dtype will be lost.

For NumPy dtypes, this will be a reference to the actual data stored in this Series or Index (assuming copy=False). Modifying the result in place will modify the data stored in the Series or Index (not that we recommend doing that).

For extension types, to_numpy() may require copying data and coercing the result to a NumPy type (possibly object), which may be expensive. When you need a no-copy reference to the underlying data, Series.array should be used instead.

This table lays out the different dtypes and default return types of to_numpy() for various dtypes within pandas.

dtype array type
category[T] ndarray[T] (same dtype as input)
period ndarray[object] (Periods)
interval ndarray[object] (Intervals)
IntegerNA ndarray[object]
datetime64[ns] datetime64[ns]
datetime64[ns, tz] ndarray[object] (Timestamps)

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(pd.Categorical(['p', 'q', 'p']))
s.to_numpy()

Output:

array(['p', 'q', 'p'], dtype=object)

Specify the dtype to control how datetime-aware data is represented. Use dtype=object to return an ndarray of pandas Timestamp objects, each with the correct tz.

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(pd.Categorical(['p', 'q', 'p']))
s = pd.Series(pd.date_range('2019', periods=4, tz="CET"))
s.to_numpy(dtype=object)

Output:

array([Timestamp('2019-01-01 00:00:00+0100', tz='CET', freq='D'),
       Timestamp('2019-01-02 00:00:00+0100', tz='CET', freq='D'),
       Timestamp('2019-01-03 00:00:00+0100', tz='CET', freq='D'),
       Timestamp('2019-01-04 00:00:00+0100', tz='CET', freq='D')],
      dtype=object)

Or dtype='datetime64[ns]' to return an ndarray of native datetime64 values. The values are converted to UTC and the timezone info is dropped.

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(pd.Categorical(['p', 'q', 'p']))
s = pd.Series(pd.date_range('2019', periods=4, tz="CET"))
s.to_numpy(dtype="datetime64[ns]")
# doctest: +ELLIPSIS

Output:

array(['2018-12-31T23:00:00.000000000', '2019-01-01T23:00:00.000000000',
       '2019-01-02T23:00:00.000000000', '2019-01-03T23:00:00.000000000'],
      dtype='datetime64[ns]')

Previous: Copy of Pandas object
Next: Access a single value for a row/column label pair in Pandas



Follow us on Facebook and Twitter for latest update.