Has anyone used Polars and Seaborn or Matplotlib together?
Asked Answered
L

2

5

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?

Link answered 8/12, 2022 at 3:57 Comment(1)
It is not recommended to directly use sns.FacetGrid. 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)Aleman
P
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')
Peridotite answered 8/12, 2022 at 12:32 Comment(0)
S
7

Now, Seaborn supports DataFrame objects from libraries other than pandas (e.g. polars) using Dataframe Exchange Protocol.

Check it out here.

Situation answered 30/11, 2023 at 14:15 Comment(0)
P
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')
Peridotite answered 8/12, 2022 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.