File tree 3 files changed +15
-1
lines changed
3 files changed +15
-1
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,8 @@ Other enhancements
25
25
Bug fixes
26
26
~~~~~~~~~
27
27
28
+ - Break reference cycle in :meth: `Index._engine ` (:issue: `27585 `)
29
+
28
30
29
31
Categorical
30
32
^^^^^^^^^^^
Original file line number Diff line number Diff line change @@ -687,7 +687,11 @@ def _cleanup(self):
687
687
@cache_readonly
688
688
def _engine (self ):
689
689
# property, for now, slow to look up
690
- return self ._engine_type (lambda : self ._ndarray_values , len (self ))
690
+
691
+ # to avoid a refernce cycle, bind `_ndarray_values` to a local variable, so
692
+ # `self` is not passed into the lambda.
693
+ _ndarray_values = self ._ndarray_values
694
+ return self ._engine_type (lambda : _ndarray_values , len (self ))
691
695
692
696
# --------------------------------------------------------------------
693
697
# Array-Like Methods
Original file line number Diff line number Diff line change 1
1
from collections import defaultdict
2
2
from datetime import datetime , timedelta
3
+ import gc
3
4
from io import StringIO
4
5
import math
5
6
import operator
@@ -2424,6 +2425,13 @@ def test_deprecated_contains(self):
2424
2425
with tm .assert_produces_warning (FutureWarning ):
2425
2426
index .contains (1 )
2426
2427
2428
+ def test_engine_reference_cycle (self ):
2429
+ # https://github.com/pandas-dev/pandas/issues/27585
2430
+ index = pd .Index ([1 , 2 , 3 ])
2431
+ nrefs_pre = len (gc .get_referrers (index ))
2432
+ index ._engine
2433
+ assert len (gc .get_referrers (index )) == nrefs_pre
2434
+
2427
2435
2428
2436
class TestMixedIntIndex (Base ):
2429
2437
# Mostly the tests from common.py for which the results differ
You can’t perform that action at this time.
0 commit comments