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 where :class:`api.extensions.ExtensionArray` could not be used in matplotlib plotting (:issue:`25587`)

.. _whatsnew_0242.enhancements:

Expand Down
12 changes: 10 additions & 2 deletions pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
from pandas.util._decorators import Appender, cache_readonly

from pandas.core.dtypes.common import (
is_hashable, is_integer, is_iterator, is_list_like, is_number)
is_hashable, is_integer, is_iterator, is_list_like, is_number,
is_extension_array_dtype)
from pandas.core.dtypes.generic import (
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 +576,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 is_extension_array_dtype(x):
x = np.asarray(x)
if is_extension_array_dtype(y):
y = np.asarray(y)

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

# GH 15516
@pytest.mark.parametrize("kwargs", [
dict(yticks=[1, 2, 3, 4]),
dict(xticks=[4, 5, 3, 2])
])
def test_integer_array_plot(self, kwargs):
# GH 25587
s = Series([4, 5, 3, 2], dtype="UInt32")
_check_plot_works(s.plot, **kwargs)

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