ENH: avoid creating reference cycle on indexing (#15746) by pitrou · Pull Request #17956 · pandas-dev/pandas (original) (raw)

You can check that this assertion erroneously passes with pandas 0.20.3 where there are definitely cyclic references by forcing an "accidental" GC:

In [8]:     def test_no_reference_cycle(happens_to_run_gc):
   ...:         import weakref, gc, sys, pandas as pd
   ...:         df = pd.DataFrame({'a': [0, 1], 'b': [2, 3]})
   ...:         refcount_before = sys.getrefcount(df)
   ...:         for name in ('loc', 'iloc', 'ix', 'at', 'iat'):
   ...:             getattr(df, name)
   ...:         refcount_after = sys.getrefcount(df)
   ...:         print("ref counts {} -> {}".format(refcount_before, refcount_after))
   ...:         wr = weakref.ref(df)
   ...:         del df
   ...:         if happens_to_run_gc:
   ...:             gc.collect()
   ...:         assert wr() is None
   ...: