Skip to content
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

BUG: Cast ExtensionArray to numpy ndarray before plot #25590

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 @@ -33,6 +33,7 @@ Fixed Regressions
- 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 regression in :meth:`DataFrame.to_csv` writing duplicate line endings with gzip compress (:issue:`25311`)
- Fixed bug where :class:`api.extensions.ExtensionArray` could not be used in matplotlib plotting (:issue:`25587`)
TomAugspurger marked this conversation as resolved.
Show resolved Hide resolved

.. _whatsnew_0242.enhancements:

Expand Down
10 changes: 8 additions & 2 deletions pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
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_extension_array_dtype, is_hashable, is_integer, is_iterator,
is_list_like, is_number)
from pandas.core.dtypes.generic import (
ABCDataFrame, ABCIndexClass, ABCMultiIndex, ABCPeriodIndex, ABCSeries)
from pandas.core.dtypes.missing import isna, notna, remove_na_arraylike
Expand Down Expand Up @@ -365,6 +366,12 @@ def _compute_plot_data(self):
if is_empty:
raise TypeError('no numeric data to plot')

# GH25587: cast ExtensionArray of pandas (IntegerArray, etc.) to
TomAugspurger marked this conversation as resolved.
Show resolved Hide resolved
# np.ndarray before plot.
for col in numeric_data:
if is_extension_array_dtype(numeric_data[col]):
numeric_data[col] = np.asarray(numeric_data[col])

self.data = numeric_data

def _make_plot(self):
Expand Down Expand Up @@ -1794,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
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")
TomAugspurger marked this conversation as resolved.
Show resolved Hide resolved

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