Skip to content

Commit d48bb2c

Browse files
author
Tom Augspurger
committed
Merge pull request pandas-dev#8177 from TomAugspurger/barplot-NaN
BUG: barplot with NaNs
2 parents 7800290 + 9bf6b59 commit d48bb2c

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

doc/source/v0.15.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -670,3 +670,4 @@ Bug Fixes
670670

671671
- Bug with kde plot and NaNs (:issue:`8182`)
672672
- Bug in ``GroupBy.count`` with float32 data type were nan values were not excluded (:issue:`8169`).
673+
- Bug with stacked barplots and NaNs (:issue:`8175`).

doc/source/visualization.rst

+38
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,44 @@ See the `matplotlib pie documenation <http://matplotlib.org/api/pyplot_api.html#
677677
678678
plt.close('all')
679679
680+
.. _visualization.missing_data
681+
682+
Plotting with Missing Data
683+
--------------------------
684+
685+
Pandas tries to be pragmatic about plotting DataFrames or Series
686+
that contain missing data. Missing values are dropped, left out, or filled
687+
depending on the plot type.
688+
689+
+----------------+--------------------------------------+
690+
| Plot Type | NaN Handling |
691+
+================+======================================+
692+
| Line | Leave gaps at NaNs |
693+
+----------------+--------------------------------------+
694+
| Line (stacked) | Fill 0's |
695+
+----------------+--------------------------------------+
696+
| Bar | Fill 0's |
697+
+----------------+--------------------------------------+
698+
| Scatter | Drop NaNs |
699+
+----------------+--------------------------------------+
700+
| Histogram | Drop NaNs (column-wise) |
701+
+----------------+--------------------------------------+
702+
| Box | Drop NaNs (column-wise) |
703+
+----------------+--------------------------------------+
704+
| Area | Fill 0's |
705+
+----------------+--------------------------------------+
706+
| KDE | Drop NaNs (column-wise) |
707+
+----------------+--------------------------------------+
708+
| Hexbin | Drop NaNs |
709+
+----------------+--------------------------------------+
710+
| Pie | Fill 0's |
711+
+----------------+--------------------------------------+
712+
713+
If any of these defaults are not what you want, or if you want to be
714+
explicit about how missing values are handled, consider using
715+
:meth:`~pandas.DataFrame.fillna` or :meth:`~pandas.DataFrame.dropna`
716+
before plotting.
717+
680718
.. _visualization.tools:
681719

682720
Plotting Tools

pandas/tests/test_graphics.py

+17
Original file line numberDiff line numberDiff line change
@@ -1479,6 +1479,23 @@ def test_bar_bottom_left(self):
14791479
result = [p.get_x() for p in ax.patches]
14801480
self.assertEqual(result, [1] * 5)
14811481

1482+
@slow
1483+
def test_bar_nan(self):
1484+
df = DataFrame({'A': [10, np.nan, 20], 'B': [5, 10, 20],
1485+
'C': [1, 2, 3]})
1486+
ax = df.plot(kind='bar')
1487+
expected = [10, 0, 20, 5, 10, 20, 1, 2, 3]
1488+
result = [p.get_height() for p in ax.patches]
1489+
self.assertEqual(result, expected)
1490+
1491+
ax = df.plot(kind='bar', stacked=True)
1492+
result = [p.get_height() for p in ax.patches]
1493+
self.assertEqual(result, expected)
1494+
1495+
result = [p.get_y() for p in ax.patches]
1496+
expected = [0.0, 0.0, 0.0, 10.0, 0.0, 20.0, 15.0, 10.0, 40.0]
1497+
self.assertEqual(result, expected)
1498+
14821499
@slow
14831500
def test_plot_scatter(self):
14841501
df = DataFrame(randn(6, 4),

pandas/tools/plotting.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -870,9 +870,11 @@ def _validate_color_args(self):
870870
" use one or the other or pass 'style' "
871871
"without a color symbol")
872872

873-
def _iter_data(self, data=None, keep_index=False):
873+
def _iter_data(self, data=None, keep_index=False, fillna=None):
874874
if data is None:
875875
data = self.data
876+
if fillna is not None:
877+
data = data.fillna(fillna)
876878

877879
from pandas.core.frame import DataFrame
878880
if isinstance(data, (Series, np.ndarray, Index)):
@@ -1780,7 +1782,7 @@ def _make_plot(self):
17801782
pos_prior = neg_prior = np.zeros(len(self.data))
17811783
K = self.nseries
17821784

1783-
for i, (label, y) in enumerate(self._iter_data()):
1785+
for i, (label, y) in enumerate(self._iter_data(fillna=0)):
17841786
ax = self._get_ax(i)
17851787
kwds = self.kwds.copy()
17861788
kwds['color'] = colors[i % ncolors]

0 commit comments

Comments
 (0)