- API reference
- DataFrame
- pandas.DataF...
pandas.DataFrame.to_numpy#
- DataFrame.to_numpy(dtype=None,copy=False,na_value=<no_default>)[source]#
Convert the DataFrame to a NumPy array.
By default, the dtype of the returned array will be the common NumPydtype of all types in the DataFrame. For example, if the dtypes are
float16andfloat32, the results dtype will befloat32.This may require copying data and coercing values, which may beexpensive.- 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=Falsedoes notensure thatto_numpy()is no-copy. Rather,copy=Trueensure 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 dtypes of the DataFrame columns.
- Returns:
- numpy.ndarray
See also
Series.to_numpySimilar method for Series.
Examples
>>>pd.DataFrame({"A":[1,2],"B":[3,4]}).to_numpy()array([[1, 3], [2, 4]])
With heterogeneous data, the lowest common type will have tobe used.
>>>df=pd.DataFrame({"A":[1,2],"B":[3.0,4.5]})>>>df.to_numpy()array([[1. , 3. ], [2. , 4.5]])
For a mix of numeric and non-numeric types, the output array willhave object dtype.
>>>df['C']=pd.date_range('2000',periods=2)>>>df.to_numpy()array([[1, 3.0, Timestamp('2000-01-01 00:00:00')], [2, 4.5, Timestamp('2000-01-02 00:00:00')]], dtype=object)