File tree 3 files changed +19
-1
lines changed
3 files changed +19
-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 1
1
from datetime import datetime , timedelta
2
+ from functools import partial
2
3
import operator
3
4
from textwrap import dedent
4
5
from typing import Union
@@ -163,6 +164,13 @@ def _new_Index(cls, d):
163
164
return cls .__new__ (cls , ** d )
164
165
165
166
167
+ def _return_obj (obj ):
168
+ """
169
+ Helper function to avoid reference cycles created by lambdas / nested functions.
170
+ """
171
+ return obj
172
+
173
+
166
174
class Index (IndexOpsMixin , PandasObject ):
167
175
"""
168
176
Immutable ndarray implementing an ordered, sliceable set. The basic object
@@ -687,7 +695,7 @@ def _cleanup(self):
687
695
@cache_readonly
688
696
def _engine (self ):
689
697
# property, for now, slow to look up
690
- return self ._engine_type (lambda : self ._ndarray_values , len (self ))
698
+ return self ._engine_type (partial ( _return_obj , self ._ndarray_values ) , len (self ))
691
699
692
700
# --------------------------------------------------------------------
693
701
# 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