Skip to content

BUG: boxplot does not work when data has datetime column #27846

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

Merged
merged 16 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ Plotting

-
-
- Bug in :meth:`DataFrame.plot` when ``kind='box'`` and data contains datatime column (:issue:`22799`)

Groupby/resample/rolling
^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
11 changes: 8 additions & 3 deletions pandas/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,14 @@ def _compute_plot_data(self):
# GH16953, _convert is needed as fallback, for ``Series``
# with ``dtype == object``
data = data._convert(datetime=True, timedelta=True)
numeric_data = data.select_dtypes(
include=[np.number, "datetime", "datetimetz", "timedelta"]
)

# GH22799, exclude datatime type for boxplot
include_type = [np.number, "datetime", "datetimetz", "timedelta"]
if self._kind == "box":
include_type.remove("datetime")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does datetimetz work? Or does it need to be excluded here too?

Copy link
Member Author

@charlesdong1991 charlesdong1991 Aug 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh, yeah, should exclude datetime-like... thanks for pointing out! @TomAugspurger


# GH22799, skip datetime type data for computation
numeric_data = data.select_dtypes(include=include_type)

try:
is_empty = numeric_data.empty
Expand Down
15 changes: 14 additions & 1 deletion pandas/tests/plotting/test_boxplot_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import pandas.util._test_decorators as td

from pandas import DataFrame, MultiIndex, Series
from pandas import DataFrame, MultiIndex, Series, date_range
from pandas.tests.plotting.common import TestPlotBase, _check_plot_works
import pandas.util.testing as tm

Expand Down Expand Up @@ -160,6 +160,19 @@ def test_fontsize(self):
df.boxplot("a", fontsize=16), xlabelsize=16, ylabelsize=16
)

def test_boxplot_numeric_data(self):
# GH 22799
df = DataFrame(
{
"a": date_range("2012-01-01", periods=100),
"b": np.random.randn(100),
"c": np.random.randn(100) + 2,
"d": date_range("2012-01-01", periods=100).astype(str),
}
)
ax = df.plot(kind="box")
assert [x.get_text() for x in ax.get_xticklabels()] == ["b", "c"]


@td.skip_if_no_mpl
class TestDataFrameGroupByPlots(TestPlotBase):
Expand Down