Skip to content

Commit a61d823

Browse files
sighingnowTomAugspurger
authored andcommitted
BUG: Cast ExtensionArray to numpy ndarray before plot (pandas-dev#25590)
* Cast `ExtensionArray` to `nd.array` before plot.
1 parent 9eec9b8 commit a61d823

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ I/O
225225
Plotting
226226
^^^^^^^^
227227

228+
- Fixed bug where :class:`api.extensions.ExtensionArray` could not be used in matplotlib plotting (:issue:`25587`)
228229
-
229230
-
230231
-

pandas/plotting/_core.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,12 @@ def _compute_plot_data(self):
365365
if is_empty:
366366
raise TypeError('no numeric data to plot')
367367

368+
# GH25587: cast ExtensionArray of pandas (IntegerArray, etc.) to
369+
# np.ndarray before plot.
370+
numeric_data = numeric_data.copy()
371+
for col in numeric_data:
372+
numeric_data[col] = np.asarray(numeric_data[col])
373+
368374
self.data = numeric_data
369375

370376
def _make_plot(self):
@@ -1794,7 +1800,6 @@ def _plot(data, x=None, y=None, subplots=False,
17941800
)
17951801
label_name = label_kw or data.columns
17961802
data.columns = label_name
1797-
17981803
plot_obj = klass(data, subplots=subplots, ax=ax, kind=kind, **kwds)
17991804

18001805
plot_obj.generate()

pandas/tests/plotting/test_frame.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import pandas as pd
1919
from pandas import (
2020
DataFrame, MultiIndex, PeriodIndex, Series, bdate_range, date_range)
21+
from pandas.core.arrays import integer_array
2122
from pandas.tests.plotting.common import (
2223
TestPlotBase, _check_plot_works, _ok_for_gaussian_kde,
2324
_skip_if_no_scipy_gaussian_kde)
@@ -144,8 +145,26 @@ def test_plot(self):
144145
result = ax.axes
145146
assert result is axes[0]
146147

147-
# GH 15516
148+
def test_integer_array_plot(self):
149+
# GH 25587
150+
arr = integer_array([1, 2, 3, 4], dtype="UInt32")
151+
152+
s = Series(arr)
153+
_check_plot_works(s.plot.line)
154+
_check_plot_works(s.plot.bar)
155+
_check_plot_works(s.plot.hist)
156+
_check_plot_works(s.plot.pie)
157+
158+
df = DataFrame({'x': arr, 'y': arr})
159+
_check_plot_works(df.plot.line)
160+
_check_plot_works(df.plot.bar)
161+
_check_plot_works(df.plot.hist)
162+
_check_plot_works(df.plot.pie, y='y')
163+
_check_plot_works(df.plot.scatter, x='x', y='y')
164+
_check_plot_works(df.plot.hexbin, x='x', y='y')
165+
148166
def test_mpl2_color_cycle_str(self):
167+
# GH 15516
149168
colors = ['C' + str(x) for x in range(10)]
150169
df = DataFrame(randn(10, 3), columns=['a', 'b', 'c'])
151170
for c in colors:

0 commit comments

Comments
 (0)