Skip to content

Commit

Permalink
BUG: Cast ExtensionArray to numpy ndarray before plot (pandas-dev#25590)
Browse files Browse the repository at this point in the history
* Cast `ExtensionArray` to `nd.array` before plot.
  • Loading branch information
sighingnow authored and TomAugspurger committed Mar 15, 2019
1 parent 9eec9b8 commit a61d823
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ I/O
Plotting
^^^^^^^^

- Fixed bug where :class:`api.extensions.ExtensionArray` could not be used in matplotlib plotting (:issue:`25587`)
-
-
-
Expand Down
7 changes: 6 additions & 1 deletion pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,12 @@ def _compute_plot_data(self):
if is_empty:
raise TypeError('no numeric data to plot')

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

self.data = numeric_data

def _make_plot(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

0 comments on commit a61d823

Please sign in to comment.