Skip to content

BUG: Cast ExtensionArray to numpy ndarray before plot #25590

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 12 commits into from
Mar 15, 2019
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ Bug Fixes
~~~~~~~~~
- Bug in :func:`to_datetime` which would raise an (incorrect) ``ValueError`` when called with a date far into the future and the ``format`` argument specified instead of raising ``OutOfBoundsDatetime`` (:issue:`23830`)
- Bug in an error message in :meth:`DataFrame.plot`. Improved the error message if non-numerics are passed to :meth:`DataFrame.plot` (:issue:`25481`)
-
- Fixed bug where :class:`api.extensions.ExtensionArray` could not be used in matplotlib plotting (:issue:`25587`)

Categorical
^^^^^^^^^^^
Expand Down
9 changes: 7 additions & 2 deletions pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,13 @@ def _compute_plot_data(self):
if is_empty:
raise TypeError('no numeric data to plot')

self.data = numeric_data
# GH25587: cast ExtensionArray of pandas (IntegerArray, etc.) to
# np.ndarray before plot.
numeric_data_np = numeric_data.copy()
for col in numeric_data:
numeric_data_np[col] = np.asarray(numeric_data[col])

self.data = numeric_data_np

def _make_plot(self):
raise AbstractMethodError(self)
Expand Down Expand Up @@ -1794,7 +1800,6 @@ def _plot(data, x=None, y=None, subplots=False,
)
label_name = label_kw or data.columns
data.columns = label_name

plot_obj = klass(data, subplots=subplots, ax=ax, kind=kind, **kwds)

plot_obj.generate()
Expand Down
21 changes: 20 additions & 1 deletion pandas/tests/plotting/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import pandas as pd
from pandas import (
DataFrame, MultiIndex, PeriodIndex, Series, bdate_range, date_range)
from pandas.core.arrays import integer_array
from pandas.tests.plotting.common import (
TestPlotBase, _check_plot_works, _ok_for_gaussian_kde,
_skip_if_no_scipy_gaussian_kde)
Expand Down Expand Up @@ -144,8 +145,26 @@ def test_plot(self):
result = ax.axes
assert result is axes[0]

# GH 15516
def test_integer_array_plot(self):
# GH 25587
arr = integer_array([1, 2, 3, 4], dtype="UInt32")

s = Series(arr)
_check_plot_works(s.plot.line)
_check_plot_works(s.plot.bar)
_check_plot_works(s.plot.hist)
_check_plot_works(s.plot.pie)

df = DataFrame({'x': arr, 'y': arr})
_check_plot_works(df.plot.line)
_check_plot_works(df.plot.bar)
_check_plot_works(df.plot.hist)
_check_plot_works(df.plot.pie, y='y')
_check_plot_works(df.plot.scatter, x='x', y='y')
_check_plot_works(df.plot.hexbin, x='x', y='y')

def test_mpl2_color_cycle_str(self):
# GH 15516
colors = ['C' + str(x) for x in range(10)]
df = DataFrame(randn(10, 3), columns=['a', 'b', 'c'])
for c in colors:
Expand Down