Skip to content

Commit 1a9769a

Browse files
committed
Test for plotting memory leak
1 parent 9d9743e commit 1a9769a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

pandas/tests/test_graphics.py

+29
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
from numpy.testing import assert_array_equal, assert_allclose
2828
from numpy.testing.decorators import slow
2929
import pandas.tools.plotting as plotting
30+
import weakref
31+
import gc
3032

3133

3234
def _skip_if_mpl_14_or_dev_boxplot():
@@ -3115,6 +3117,33 @@ def _check_errorbar_color(containers, expected, has_err='has_xerr'):
31153117
self._check_has_errorbars(ax, xerr=0, yerr=1)
31163118
_check_errorbar_color(ax.containers, 'green', has_err='has_yerr')
31173119

3120+
def test_memory_leak(self):
3121+
""" Check that every plot type gets properly collected. """
3122+
import matplotlib.pyplot as plt
3123+
results = {}
3124+
for kind in plotting._plot_klass.keys():
3125+
args = {}
3126+
if kind in ['hexbin', 'scatter', 'pie']:
3127+
df = self.hexbin_df
3128+
args = {'x': 'A', 'y': 'B'}
3129+
elif kind == 'area':
3130+
df = self.tdf.abs()
3131+
else:
3132+
df = self.tdf
3133+
3134+
# Use a weakref so we can see if the object gets collected without
3135+
# also preventing it from being collected
3136+
results[kind] = weakref.proxy(df.plot(kind=kind, **args))
3137+
3138+
# have matplotlib delete all the figures
3139+
plt.close('all')
3140+
# force a garbage collection
3141+
gc.collect()
3142+
for key in results:
3143+
# check that every plot was collected
3144+
with tm.assertRaises(ReferenceError):
3145+
# need to actually access something to get an error
3146+
results[key].lines
31183147

31193148
@tm.mplskip
31203149
class TestDataFrameGroupByPlots(TestPlotBase):

0 commit comments

Comments
 (0)