Skip to content

Commit

Permalink
BUG: boxplot does not work when data has datetime column (#27846)
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesdong1991 authored and TomAugspurger committed Aug 13, 2019
1 parent d32d464 commit b623a9d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
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:`Series.plot` not able to plot boolean values (:issue:`23719`)
-
- Bug in :meth:`DataFrame.plot` when ``kind='box'`` and data contains datetime or timedelta data. These types are now automatically dropped (:issue:`22799`)

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

# GH23719, allow plotting boolean
if self.include_bool is True:
select_include_type.append(np.bool_)
numeric_data = data.select_dtypes(include=select_include_type)
include_type.append(np.bool_)

# GH22799, exclude datatime-like type for boxplot
exclude_type = None
if self._kind == "box":
# TODO: change after solving issue 27881
include_type = [np.number]
exclude_type = ["timedelta"]

numeric_data = data.select_dtypes(include=include_type, exclude=exclude_type)

try:
is_empty = numeric_data.empty
Expand Down
17 changes: 16 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, timedelta_range
from pandas.tests.plotting.common import TestPlotBase, _check_plot_works
import pandas.util.testing as tm

Expand Down Expand Up @@ -160,6 +160,21 @@ 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),
"e": date_range("2012-01-01", periods=100, tz="UTC"),
"f": timedelta_range("1 days", periods=100),
}
)
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

0 comments on commit b623a9d

Please sign in to comment.