Search code examples
pythontypesseabornpython-polars

Has anyone used Polars and Seaborn or Matplotlib together?


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?


Solution

  • 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')