MCPcopy
hub / github.com/pandas-dev/pandas / to_numpy

Method to_numpy

pandas/core/frame.py:2020–2085  ·  view source on GitHub ↗

Convert the DataFrame to a NumPy array. By default, the dtype of the returned array will be the common NumPy dtype of all types in the DataFrame. For example, if the dtypes are ``float16`` and ``float32``, the results dtype will be ``float32``. This may requ

(
        self,
        dtype: npt.DTypeLike | None = None,
        copy: bool = False,
        na_value: object = lib.no_default,
    )

Source from the content-addressed store, hash-verified

2018 return cls(realdata, index=index, columns=columns, dtype=dtype)
2019
2020 def to_numpy(
2021 self,
2022 dtype: npt.DTypeLike | None = None,
2023 copy: bool = False,
2024 na_value: object = lib.no_default,
2025 ) -> np.ndarray:
2026 """
2027 Convert the DataFrame to a NumPy array.
2028
2029 By default, the dtype of the returned array will be the common NumPy
2030 dtype of all types in the DataFrame. For example, if the dtypes are
2031 ``float16`` and ``float32``, the results dtype will be ``float32``.
2032 This may require copying data and coercing values, which may be
2033 expensive.
2034
2035 Parameters
2036 ----------
2037 dtype : str or numpy.dtype, optional
2038 The dtype to pass to :meth:`numpy.asarray`.
2039 copy : bool, default False
2040 Whether to ensure that the returned value is not a view on
2041 another array. Note that ``copy=False`` does not *ensure* that
2042 ``to_numpy()`` is no-copy. Rather, ``copy=True`` ensure that
2043 a copy is made, even if not strictly necessary.
2044 na_value : Any, optional
2045 The value to use for missing values. The default value depends
2046 on `dtype` and the dtypes of the DataFrame columns.
2047
2048 Returns
2049 -------
2050 numpy.ndarray
2051 The NumPy array representing the values in the DataFrame.
2052
2053 See Also
2054 --------
2055 Series.to_numpy : Similar method for Series.
2056
2057 Examples
2058 --------
2059 >>> pd.DataFrame({"A": [1, 2], "B": [3, 4]}).to_numpy()
2060 array([[1, 3],
2061 [2, 4]])
2062
2063 With heterogeneous data, the lowest common type will have to
2064 be used.
2065
2066 >>> df = pd.DataFrame({"A": [1, 2], "B": [3.0, 4.5]})
2067 >>> df.to_numpy()
2068 array([[1. , 3. ],
2069 [2. , 4.5]])
2070
2071 For a mix of numeric and non-numeric types, the output array will
2072 have object dtype.
2073
2074 >>> df["C"] = pd.date_range("2000", periods=2)
2075 >>> df.to_numpy()
2076 array([[1, 3.0, Timestamp('2000-01-01 00:00:00')],
2077 [2, 4.5, Timestamp('2000-01-02 00:00:00')]], dtype=object)

Calls 2

dtypeMethod · 0.45
as_arrayMethod · 0.45