Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relplot gives a cryptic error message with col='size' #2488

Closed
jhncls opened this issue Feb 25, 2021 · 2 comments · Fixed by #2581
Closed

relplot gives a cryptic error message with col='size' #2488

jhncls opened this issue Feb 25, 2021 · 2 comments · Fixed by #2581
Labels
Milestone

Comments

@jhncls
Copy link

jhncls commented Feb 25, 2021

In seaborn 0.11.1:

import seaborn as sns
tips = sns.load_dataset('tips')
sns.relplot(x='total_bill', y='tip', col='size', data=tips)

Generates "ValueError: could not broadcast input array from shape (244,2) into shape (244,)"
col='size' doesn't seem to be interpreted as a column name.

@mwaskom mwaskom added the bug label Feb 25, 2021
@mwaskom mwaskom added this to the v0.12.0 milestone Feb 25, 2021
@mwaskom
Copy link
Owner

mwaskom commented Feb 25, 2021

Thanks for reporting. Here's the full traceback that I see:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~/miniconda3/envs/py39/lib/python3.9/site-packages/seaborn/_core.py in categorical_order(vector, order)
   1471             try:
-> 1472                 order = vector.cat.categories
   1473             except (TypeError, AttributeError):

~/miniconda3/envs/py39/lib/python3.9/site-packages/pandas/core/generic.py in __getattr__(self, name)
   5461                 return self[name]
-> 5462             return object.__getattribute__(self, name)
   5463 

AttributeError: 'DataFrame' object has no attribute 'cat'

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
~/miniconda3/envs/py39/lib/python3.9/site-packages/seaborn/_core.py in categorical_order(vector, order)
   1475                 try:
-> 1476                     order = vector.unique()
   1477                 except AttributeError:

~/miniconda3/envs/py39/lib/python3.9/site-packages/pandas/core/generic.py in __getattr__(self, name)
   5461                 return self[name]
-> 5462             return object.__getattribute__(self, name)
   5463 

AttributeError: 'DataFrame' object has no attribute 'unique'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-15-c57b6168b51b> in <module>
      5 
      6 #Plotting our subplots dividing with smoker
----> 7 sns.relplot(x = "total_bill", y = "tip", hue = "smoker", col = "size", data = tips)
      8 
      9 #Showing our plots in a proper format

~/miniconda3/envs/py39/lib/python3.9/site-packages/seaborn/_decorators.py in inner_f(*args, **kwargs)
     44             )
     45         kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 46         return f(**kwargs)
     47     return inner_f
     48 

~/miniconda3/envs/py39/lib/python3.9/site-packages/seaborn/relational.py in relplot(x, y, hue, size, style, data, row, col, col_wrap, row_order, col_order, palette, hue_order, hue_norm, sizes, size_order, size_norm, markers, dashes, style_order, legend, kind, height, aspect, facet_kws, units, **kwargs)
   1013     facet_kws = {} if facet_kws is None else facet_kws.copy()
   1014     facet_kws.update(grid_kws)
-> 1015     g = FacetGrid(
   1016         data=full_data,
   1017         col_wrap=col_wrap, row_order=row_order, col_order=col_order,

~/miniconda3/envs/py39/lib/python3.9/site-packages/seaborn/_decorators.py in inner_f(*args, **kwargs)
     44             )
     45         kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 46         return f(**kwargs)
     47     return inner_f
     48 

~/miniconda3/envs/py39/lib/python3.9/site-packages/seaborn/axisgrid.py in __init__(self, data, row, col, hue, col_wrap, sharex, sharey, height, aspect, palette, row_order, col_order, hue_order, hue_kws, dropna, legend_out, despine, margin_titles, xlim, ylim, subplot_kws, gridspec_kws, size)
    334             col_names = []
    335         else:
--> 336             col_names = categorical_order(data[col], col_order)
    337 
    338         # Additional dict of kwarg -> list of values for mapping the hue var

~/miniconda3/envs/py39/lib/python3.9/site-packages/seaborn/_core.py in categorical_order(vector, order)
   1476                     order = vector.unique()
   1477                 except AttributeError:
-> 1478                     order = pd.unique(vector)
   1479 
   1480                 if variable_type(vector) == "numeric":

~/miniconda3/envs/py39/lib/python3.9/site-packages/pandas/core/algorithms.py in unique(values)
    395     array([('a', 'b'), ('b', 'a'), ('a', 'c')], dtype=object)
    396     """
--> 397     values = _ensure_arraylike(values)
    398 
    399     if is_extension_array_dtype(values):

~/miniconda3/envs/py39/lib/python3.9/site-packages/pandas/core/algorithms.py in _ensure_arraylike(values)
    239             if isinstance(values, tuple):
    240                 values = list(values)
--> 241             values = construct_1d_object_array_from_listlike(values)
    242         else:
    243             values = np.asarray(values)

~/miniconda3/envs/py39/lib/python3.9/site-packages/pandas/core/dtypes/cast.py in construct_1d_object_array_from_listlike(values)
   1636     # making a 1D array that contains list-likes is a bit tricky:
   1637     result = np.empty(len(values), dtype="object")
-> 1638     result[:] = values
   1639     return result
   1640 

ValueError: could not broadcast input array from shape (244,2) into shape (244)

@mwaskom
Copy link
Owner

mwaskom commented Feb 25, 2021

OK I think the answer is that it's a bug caused by the external column name colliding with one of the internal variable names (because you can do relplot(size=...). I haven't tracked it down completely but I suspect this has to do with the relatively complex relplot-specific logic of joining the internal data structure with the faceting columns.

I would like to simplify that logic in general so I am hoping this bug will get fixed indirectly as part of that process.

Current workaround is to rename the column something that doesn't collide with the seaborn variable names:

sns.relplot(x="total_bill", y="tip", hue="smoker", col=tips["size"].rename(" size"), data=tips)

mwaskom added a commit that referenced this issue May 11, 2021
* Avoid error from relplot faceting variable name collision

Fixes #2488

* Make data attribute on output FacetGrid have original column names
mwaskom added a commit that referenced this issue Aug 6, 2021
* Avoid error from relplot faceting variable name collision

Fixes #2488

* Make data attribute on output FacetGrid have original column names

(cherry picked from commit f884505)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants