polars.DataFrame.to_pandas#

DataFrame.to_pandas(
*args: Any,
use_pyarrow_extension_array: bool = False,
**kwargs: Any,
) DataFrame[source]#

Cast to a pandas DataFrame.

This requires that pandas and pyarrow are installed. This operation clones data, unless use_pyarrow_extension_array=True.

Parameters:
use_pyarrow_extension_array

Use PyArrow backed-extension arrays instead of numpy arrays for each column 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 arrays if that operation is not supported by pyarrow compute functions.

**kwargs

Arguments will be sent to pyarrow.Table.to_pandas().

Returns:
pandas.DataFrame

Examples

>>> import pandas
>>> df1 = pl.DataFrame(
...     {
...         "foo": [1, 2, 3],
...         "bar": [6, 7, 8],
...         "ham": ["a", "b", "c"],
...     }
... )
>>> pandas_df1 = df1.to_pandas()
>>> type(pandas_df1)
<class 'pandas.core.frame.DataFrame'>
>>> pandas_df1.dtypes
foo     int64
bar     int64
ham    object
dtype: object
>>> df2 = pl.DataFrame(
...     {
...         "foo": [1, 2, None],
...         "bar": [6, None, 8],
...         "ham": [None, "b", "c"],
...     }
... )
>>> pandas_df2 = df2.to_pandas()
>>> pandas_df2
   foo  bar   ham
0  1.0  6.0  None
1  2.0  NaN     b
2  NaN  8.0     c
>>> pandas_df2.dtypes
foo    float64
bar    float64
ham     object
dtype: object
>>> pandas_df2_pa = df2.to_pandas(
...     use_pyarrow_extension_array=True
... )  
>>> pandas_df2_pa  
    foo   bar   ham
0     1     6  <NA>
1     2  <NA>     b
2  <NA>     8     c
>>> pandas_df2_pa.dtypes  
foo           int64[pyarrow]
bar           int64[pyarrow]
ham    large_string[pyarrow]
dtype: object