Skip to content

Commit 35e3933

Browse files
sinhrksjreback
authored andcommitted
TST/BUG: Categorical dtypes cause error when attempting stacked bar plot
closes #13019 Author: sinhrks <[email protected]> Closes #13038 from sinhrks/plot_bar_categorical and squashes the following commits: dc57a6c [sinhrks] TST/BUG: Categorical dtypes cause error when attempting stacked bar plot
1 parent 84725fa commit 35e3933

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

pandas/tests/test_graphics.py

+26
Original file line numberDiff line numberDiff line change
@@ -2100,6 +2100,32 @@ def test_bar_nan(self):
21002100
expected = [0.0, 0.0, 0.0, 10.0, 0.0, 20.0, 15.0, 10.0, 40.0]
21012101
self.assertEqual(result, expected)
21022102

2103+
@slow
2104+
def test_bar_categorical(self):
2105+
# GH 13019
2106+
df1 = pd.DataFrame(np.random.randn(6, 5),
2107+
index=pd.Index(list('ABCDEF')),
2108+
columns=pd.Index(list('abcde')))
2109+
# categorical index must behave the same
2110+
df2 = pd.DataFrame(np.random.randn(6, 5),
2111+
index=pd.CategoricalIndex(list('ABCDEF')),
2112+
columns=pd.CategoricalIndex(list('abcde')))
2113+
2114+
for df in [df1, df2]:
2115+
ax = df.plot.bar()
2116+
ticks = ax.xaxis.get_ticklocs()
2117+
tm.assert_numpy_array_equal(ticks, np.array([0, 1, 2, 3, 4, 5]))
2118+
self.assertEqual(ax.get_xlim(), (-0.5, 5.5))
2119+
# check left-edge of bars
2120+
self.assertEqual(ax.patches[0].get_x(), -0.25)
2121+
self.assertEqual(ax.patches[-1].get_x(), 5.15)
2122+
2123+
ax = df.plot.bar(stacked=True)
2124+
tm.assert_numpy_array_equal(ticks, np.array([0, 1, 2, 3, 4, 5]))
2125+
self.assertEqual(ax.get_xlim(), (-0.5, 5.5))
2126+
self.assertEqual(ax.patches[0].get_x(), -0.25)
2127+
self.assertEqual(ax.patches[-1].get_x(), 4.75)
2128+
21032129
@slow
21042130
def test_plot_scatter(self):
21052131
df = DataFrame(randn(6, 4),

0 commit comments

Comments
 (0)