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

BUG: DataFrameGroupBy.boxplot with subplots=False fails for object columns #43480

Closed
3 tasks done
sarming opened this issue Sep 9, 2021 · 3 comments · Fixed by #44003
Closed
3 tasks done

BUG: DataFrameGroupBy.boxplot with subplots=False fails for object columns #43480

sarming opened this issue Sep 9, 2021 · 3 comments · Fixed by #44003
Assignees
Milestone

Comments

@sarming
Copy link

sarming commented Sep 9, 2021

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the master branch of pandas.

Reproducible Example

import pandas as pd
df = pd.DataFrame({'cat':list('aabbc'), 
                    'v':range(5)}, dtype=object)
df.groupby('cat').boxplot(subplots=False, column='v')

results in the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/src/pandas/pandas/plotting/_core.py", line 604, in boxplot_frame_groupby
    return plot_backend.boxplot_frame_groupby(
  File "/src/pandas/pandas/plotting/_matplotlib/boxplot.py", line 485, in boxplot_frame_groupby
    ret = df.boxplot(
  File "/src/pandas/pandas/plotting/_core.py", line 511, in boxplot_frame
    return plot_backend.boxplot_frame(
  File "/Users/ian/src/pandas/pandas/plotting/_matplotlib/boxplot.py", line 414, in boxplot_frame
    ax = boxplot(
  File "/src/pandas/pandas/plotting/_matplotlib/boxplot.py", line 391, in boxplot
    data = data[columns]
  File "/src/pandas/pandas/core/frame.py", line 3504, in __getitem__
    indexer = self.columns._get_indexer_strict(key, "columns")[1]
  File "/src/pandas/pandas/core/indexes/multi.py", line 2559, in _get_indexer_strict
    return super()._get_indexer_strict(key, axis_name)
  File "/src/pandas/pandas/core/indexes/base.py", line 5510, in _get_indexer_strict
    self._raise_if_missing(keyarr, indexer, axis_name)
  File "/src/pandas/pandas/core/indexes/multi.py", line 2579, in _raise_if_missing
    return super()._raise_if_missing(key, indexer, axis_name)
  File "/src/pandas/pandas/core/indexes/base.py", line 5570, in _raise_if_missing
    raise KeyError(f"None of [{key}] are in the [{axis_name}]")
KeyError: "None of [MultiIndex([('a', 'v'),\n            ('b', 'v'),\n            ('c', 'v')],\n           )] are in the [columns]"

Issue Description

This might be related to #16748

Expected Behavior

The same as:

import pandas as pd
df = pd.DataFrame({'cat':list('aabbc'), 
                    'v':range(5)})
df.groupby('cat').boxplot(subplots=False, column='v')

Installed Versions

INSTALLED VERSIONS ------------------ commit : a951998 python : 3.9.6.final.0 python-bits : 64 OS : Darwin OS-release : 20.6.0 Version : Darwin Kernel Version 20.6.0: Wed Jun 23 00:26:31 PDT 2021; root:xnu-7195.141.2~5/RELEASE_X86_64 machine : x86_64 processor : i386 byteorder : little LC_ALL : None LANG : None LOCALE : None.UTF-8

pandas : 1.4.0.dev0+634.ga951998314
numpy : 1.20.3
pytz : 2021.1
dateutil : 2.8.2
pip : 21.2.4
setuptools : 52.0.0.post20210125
Cython : 0.29.24
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.4.2
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@sarming sarming added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 9, 2021
@mroeschke mroeschke added Groupby Visualization plotting and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 30, 2021
@brendandrury
Copy link
Contributor

brendandrury commented Oct 9, 2021

I'll take this. It seems like applying to_numeric before calling get_numeric_data here if dtype is object should fix the problem.

@brendandrury
Copy link
Contributor

@jreback has raised the issue that this may not necessarily be desired behavior. Based on the documentation, boxplot isn’t supported on object columns, and it should be up to the user to coerce them to numbers if that’s what they want.

If that is the case, it might make sense to check for this somewhere, explicitly raise an error indicating that this is not a supported use of boxplot, and suggest that the user use to_numeric if they have numeric data that is mistakenly set to type object. That seems easier to understand than the cryptic error above.

@jreback
Copy link
Contributor

jreback commented Oct 21, 2021

let me clarify
if there aren't any numerical columns then simply raise like all other plotting routines

@jreback jreback added this to the 1.4 milestone Nov 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants