Seaborn futurewarning caused by pandas dataframe
Asked Answered
S

3

9

I am new to data analysis. I am currently using seaborn 0.13.1 along with pandas 2.2.0 and I was messing around with the following code:

import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt

exam_scores1 = [62.58, 67.63, 81.37, 52.53, 62.98, 72.15, 59.05, 73.85, 97.24, 76.81, 89.34, 74.44, 68.52, 85.13, 90.75, 70.29, 75.62, 85.38, 77.82, 98.31, 79.08, 61.72, 71.33, 80.77, 80.31, 78.16, 61.15, 64.99, 72.67, 78.94]
exam_scores2 = [72.38, 71.28, 79.24, 83.86, 84.42, 79.38, 75.51, 76.63, 81.48,78.81,79.23,74.38,79.27,81.07,75.42,90.35,82.93,86.74,81.33,95.1,86.57,83.66,85.58,81.87,92.14,72.15,91.64,74.21,89.04,76.54,81.9,96.5,80.05,74.77,72.26,73.23,92.6,66.22,70.09,77.2]

data = np.concatenate([np.array(exam_scores1), np.array(exam_scores2)])
labels = ['1st Yr Teaching'] * len(exam_scores1) + ['2nd Yr Teaching'] * len(exam_scores2)
df = pd.DataFrame({'Score': data, 'Teaching Year': labels})

plt.figure(figsize=(10, 8))
sns.histplot(data=df, x='Score', hue=df['Teaching Year'], bins=12, kde=True, stat='density', linewidth=2)
plt.title("Final Exam Score Distribution")
plt.xlabel("Percentage")
plt.ylabel("Density")
plt.legend(title='Teaching Year')
plt.savefig('my_histogram_seaborn.png')
plt.show()

The plot is generated successfully but I am getting the following futurewarning from seaborn:

FutureWarning: When grouping with a length-1 list-like, you will need to pass a length-1 tuple to get_group in a future version of pandas. Pass `(name,)` instead of `name` to silence this warning.
  data_subset = grouped_data.get_group(pd_key)

What does this mean? TIA

I tried to understand the thing first, as I am new I can't get a hold of the thing

Sheeree answered 25/1 at 19:13 Comment(4)
What version of pandas? That's a pandas warning.Tumefaction
Pandas version 2.2.0Sheeree
pandas.pydata.org/docs/dev/whatsnew/v2.2.0.html#deprecationsTumefaction
The warning has been dealt with in seaborn 0.13.2, released a few hours ago.Polash
E
11

I got the same warning, and it was solved after updating seaborn to version 0.13.2 (or later).

You can upgrade seaborn by running:

pip install --upgrade seaborn
Eisenhart answered 29/1 at 10:26 Comment(0)
H
0

If the warnings persist. Use the following code to suppress the warnings:

import warnings # To suppress some warnings
 
# Suppress the specific FutureWarning
warnings.filterwarnings("ignore", category=FutureWarning, module="seaborn")
Hovey answered 2/6 at 11:39 Comment(0)
M
0

This warning simply implies that the code which you have wrote currently is correct as per current scenario but this will not work when pandas library is updated in the future.

length-1 list is simply a single-element list or single-element array.

This warning simply indicates that in this code you are passing a length-1 list but in future you would need to pass length-1 tuple instead of length-1 list.

Now you might question that the given warning is talking about some type of grouping, but in the code we are not grouping any data?

Actually we might actually be grouping data indirectly. I am not sure but in this line of code

sns.histplot(data=df, x='Score', hue=df['Teaching Year'], bins=12, kde=True, stat='density', linewidth=2)

we might be grouping data based on Teaching year while plotting the graph and providing hue=df['Teaching Year'].

To solve this warning try using hue=df('Teaching Year',)

Meehan answered 2/6 at 12:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.