polars.DataFrame.to_pandas
-
Convert this DataFrame to a pandas DataFrame.
This operation copies data if
use_pyarrow_extension_arrayis not enabled.- Parameters:
-
- use_pyarrow_extension_array
-
Use PyArrow-backed extension arrays instead of NumPy arrays for the columns of the pandas DataFrame. This allows zero copy operations and preservation of null values. Subsequent operations on the resulting pandas DataFrame may trigger conversion to NumPy if those operations are not supported by PyArrow compute functions.
- **kwargs
-
Additional keyword arguments to be passed to
pyarrow.Table.to_pandas().
- Returns:
-
-
pandas.DataFrame
-
Notes
This operation requires that both
pandasandpyarroware installed.Examples
>>> df = pl.DataFrame( ... { ... "foo": [1, 2, 3], ... "bar": [6.0, 7.0, 8.0], ... "ham": ["a", "b", "c"], ... } ... ) >>> df.to_pandas() foo bar ham 0 1 6.0 a 1 2 7.0 b 2 3 8.0 cNull values in numeric columns are converted to
NaN.>>> df = pl.DataFrame( ... { ... "foo": [1, 2, None], ... "bar": [6.0, None, 8.0], ... "ham": [None, "b", "c"], ... } ... ) >>> df.to_pandas() foo bar ham 0 1.0 6.0 NaN 1 2.0 NaN b 2 NaN 8.0 cPass
use_pyarrow_extension_array=Trueto get a pandas DataFrame with columns backed by PyArrow extension arrays. This will preserve null values.>>> df.to_pandas(use_pyarrow_extension_array=True) foo bar ham 0 1 6.0 <NA> 1 2 <NA> b 2 <NA> 8.0 c >>> _.dtypes foo int64[pyarrow] bar double[pyarrow] ham large_string[pyarrow] dtype: object
DataFrame.to_pandas(
*,
use_pyarrow_extension_array: bool = False,
**kwargs: Any,
) → DataFrame
© 2020 Ritchie Vink
© 2022 Polars contributors
Licensed under the MIT License.
https://docs.pola.rs/api/python/stable/reference/dataframe/api/polars.DataFrame.to_pandas.html