Skip to content

Commit b623a9d

Browse files
charlesdong1991TomAugspurger
authored andcommitted
BUG: boxplot does not work when data has datetime column (#27846)
1 parent d32d464 commit b623a9d

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ Plotting
165165

166166
- Bug in :meth:`Series.plot` not able to plot boolean values (:issue:`23719`)
167167
-
168+
- Bug in :meth:`DataFrame.plot` when ``kind='box'`` and data contains datetime or timedelta data. These types are now automatically dropped (:issue:`22799`)
168169

169170
Groupby/resample/rolling
170171
^^^^^^^^^^^^^^^^^^^^^^^^

pandas/plotting/_matplotlib/core.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -402,12 +402,20 @@ def _compute_plot_data(self):
402402
# GH16953, _convert is needed as fallback, for ``Series``
403403
# with ``dtype == object``
404404
data = data._convert(datetime=True, timedelta=True)
405-
select_include_type = [np.number, "datetime", "datetimetz", "timedelta"]
405+
include_type = [np.number, "datetime", "datetimetz", "timedelta"]
406406

407407
# GH23719, allow plotting boolean
408408
if self.include_bool is True:
409-
select_include_type.append(np.bool_)
410-
numeric_data = data.select_dtypes(include=select_include_type)
409+
include_type.append(np.bool_)
410+
411+
# GH22799, exclude datatime-like type for boxplot
412+
exclude_type = None
413+
if self._kind == "box":
414+
# TODO: change after solving issue 27881
415+
include_type = [np.number]
416+
exclude_type = ["timedelta"]
417+
418+
numeric_data = data.select_dtypes(include=include_type, exclude=exclude_type)
411419

412420
try:
413421
is_empty = numeric_data.empty

pandas/tests/plotting/test_boxplot_method.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import pandas.util._test_decorators as td
1111

12-
from pandas import DataFrame, MultiIndex, Series
12+
from pandas import DataFrame, MultiIndex, Series, date_range, timedelta_range
1313
from pandas.tests.plotting.common import TestPlotBase, _check_plot_works
1414
import pandas.util.testing as tm
1515

@@ -160,6 +160,21 @@ def test_fontsize(self):
160160
df.boxplot("a", fontsize=16), xlabelsize=16, ylabelsize=16
161161
)
162162

163+
def test_boxplot_numeric_data(self):
164+
# GH 22799
165+
df = DataFrame(
166+
{
167+
"a": date_range("2012-01-01", periods=100),
168+
"b": np.random.randn(100),
169+
"c": np.random.randn(100) + 2,
170+
"d": date_range("2012-01-01", periods=100).astype(str),
171+
"e": date_range("2012-01-01", periods=100, tz="UTC"),
172+
"f": timedelta_range("1 days", periods=100),
173+
}
174+
)
175+
ax = df.plot(kind="box")
176+
assert [x.get_text() for x in ax.get_xticklabels()] == ["b", "c"]
177+
163178

164179
@td.skip_if_no_mpl
165180
class TestDataFrameGroupByPlots(TestPlotBase):

0 commit comments

Comments
 (0)