4

Has anyone used a Polars dataframe with Seaborn to graph something? I've been working through a notebook on Kaggle that used Pandas, and I wanted to refactor it to Polars.

The dataframe I'm working with looks like this:

PassengerID (i64) Survived (i64) Pclass (i64) Name (str) ... Ticket (str) Fare (f64) Cabin (str) Embarked (str) Age (f64)
1 0 3 your name here ... A/5 21171 7.25 null S 24
... ... ... ... ... ... ... ... ... ...

Kaggle has me making a histogram with the following code:

g = sns.FacetGrid(train_df, col='Survived')
g.map(plt.hist, 'Age', bins=20)

When I run these two lines I get the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/seaborn/axisgrid.py", line 678, in map
    for (row_i, col_j, hue_k), data_ijk in self.facet_data():
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/seaborn/axisgrid.py", line 632, in facet_data
    data_ijk = data[row & col & hue & self._not_na]
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/polars/internals/series/series.py", line 906, in __array_ufunc__
    args.append(arg.view(ignore_nulls=True))
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/polars/internals/series/series.py", line 2680, in view
    ptr_type = dtype_to_ctype(self.dtype)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/polars/datatypes.py", line 550, in dtype_to_ctype
    raise NotImplementedError(
NotImplementedError: Conversion of polars data type <class 'polars.datatypes.Boolean'> to C-type not implemented.

I don't have any boolean datatypes in my dataframe, so I'm not sure what to do about this error. Any ideas?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Jordan
  • 45
  • 5
  • It is not recommended to directly use [`sns.FacetGrid`](https://seaborn.pydata.org/generated/seaborn.FacetGrid.html). The correct methods are `ax = sns.histplot(data=train_df.to_pandas(), x='Age', col='Survived', bins=20)` or `g = sns.displot(data=train_df.to_pandas(), x='Age', col='Survived', bins=20)` – Trenton McKinney Dec 08 '22 at 20:12

1 Answers1

5

seaborn doesn't accept a polars dataframe as an input. You just have to use to_pandas()

so change g = sns.FacetGrid(train_df, col='Survived') to

g = sns.FacetGrid(train_df.to_pandas(), col='Survived')
Dean MacGregor
  • 11,847
  • 9
  • 34
  • 72