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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Fixed Regressions
- Fixed regression in ``IntervalDtype`` construction where passing an incorrect string with 'Interval' as a prefix could result in a ``RecursionError``. (:issue:`25338`)
- Fixed regression in :class:`Categorical`, where constructing it from a categorical ``Series`` and an explicit ``categories=`` that differed from that in the ``Series`` created an invalid object which could trigger segfaults. (:issue:`25318`)
- Fixed pip installing from source into an environment without NumPy (:issue:`25193`)
- Fixed bug that :class:`ExtensionArray` cannot be used to matplotlib plotting (:issue:`25587`)

.. _whatsnew_0242.enhancements:

Expand Down
9 changes: 8 additions & 1 deletion pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
ABCDataFrame, ABCIndexClass, ABCMultiIndex, ABCPeriodIndex, ABCSeries)
from pandas.core.dtypes.missing import isna, notna, remove_na_arraylike

from pandas.core.arrays import ExtensionArray
from pandas.core.base import PandasObject
import pandas.core.common as com
from pandas.core.config import get_option
Expand Down Expand Up @@ -574,6 +575,13 @@ def _get_xticks(self, convert_period=False):

@classmethod
def _plot(cls, ax, x, y, style=None, is_errorbar=False, **kwds):
# GH25587: cast ExtensionArray of pandas (IntegerArray, etc.) to
# np.ndarray before plot.
if isinstance(x, ExtensionArray):
x = x.__array__()
if isinstance(y, ExtensionArray):
y = y.__array__()

mask = isna(y)
if mask.any():
y = np.ma.array(y)
Expand Down Expand Up @@ -1792,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
6 changes: 6 additions & 0 deletions pandas/tests/plotting/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ def test_plot(self):
result = ax.axes
assert result is axes[0]

# GH 25587
def test_integer_array_plot(self):
s = Series([4, 5, 3, 2], dtype="UInt32")
_check_plot_works(s.plot, yticks=[1, 2, 3, 4])
_check_plot_works(s.plot, xticks=[4, 5, 3, 2])

# GH 15516
def test_mpl2_color_cycle_str(self):
colors = ['C' + str(x) for x in range(10)]
Expand Down