- API reference
- Series
- pandas.Serie...
pandas.Series.to_numpy#
- Series.to_numpy(dtype=None,copy=False,na_value=<no_default>,**kwargs)[source]#
A NumPy ndarray representing the values in this Series or Index.
- Parameters:
- dtypestr or numpy.dtype, optional
The dtype to pass to
numpy.asarray()
.- copybool, default False
Whether to ensure that the returned value is not a view onanother array. Note that
copy=False
does notensure thatto_numpy()
is no-copy. Rather,copy=True
ensure thata copy is made, even if not strictly necessary.- na_valueAny, optional
The value to use for missing values. The default value dependsondtype and the type of the array.
- **kwargs
Additional keywords passed through to the
to_numpy
methodof the underlying array (for extension arrays).
- Returns:
- numpy.ndarray
See also
Series.array
Get the actual data stored within.
Index.array
Get the actual data stored within.
DataFrame.to_numpy
Similar method for DataFrame.
Notes
The returned array will be the same up to equality (values equalinself will be equal in the returned array; likewise for valuesthat are not equal). Whenself contains an ExtensionArray, thedtype may be different. For example, for a category-dtype Series,
to_numpy()
will return a NumPy array and the categorical dtypewill be lost.For NumPy dtypes, this will be a reference to the actual data storedin this Series or Index (assuming
copy=False
). Modifying the resultin place will modify the data stored in the Series or Index (not thatwe recommend doing that).For extension types,
to_numpy()
may require copying data andcoercing the result to a NumPy type (possibly object), which may beexpensive. 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)
Examples
>>>ser=pd.Series(pd.Categorical(['a','b','a']))>>>ser.to_numpy()array(['a', 'b', 'a'], dtype=object)
Specify thedtype to control how datetime-aware data is represented.Use
dtype=object
to return an ndarray of pandasTimestamp
objects, each with the correcttz
.>>>ser=pd.Series(pd.date_range('2000',periods=2,tz="CET"))>>>ser.to_numpy(dtype=object)array([Timestamp('2000-01-01 00:00:00+0100', tz='CET'), Timestamp('2000-01-02 00:00:00+0100', tz='CET')], dtype=object)
Or
dtype='datetime64[ns]'
to return an ndarray of nativedatetime64 values. The values are converted to UTC and the timezoneinfo is dropped.>>>ser.to_numpy(dtype="datetime64[ns]")...array(['1999-12-31T23:00:00.000000000', '2000-01-01T23:00:00...'], dtype='datetime64[ns]')