diff --git a/zipline/utils/pandas_utils.py b/zipline/utils/pandas_utils.py index 1b81897a..633960e1 100644 --- a/zipline/utils/pandas_utils.py +++ b/zipline/utils/pandas_utils.py @@ -168,19 +168,25 @@ def ignore_pandas_nan_categorical_warning(): yield +_INDEXER_NAMES = [ + '_' + name for (name, _) in pd.core.indexing.get_indexers_list() +] + + def clear_dataframe_indexer_caches(df): """ Clear cached attributes from a pandas DataFrame. - By default pandas memoizes `iloc`, `loc` objects on DataFrames, resulting - in refcycles that can lead to unexpectedly long-lived DataFrames. This - function attempts to clear those cycles. + By default pandas memoizes indexers (`iloc`, `loc`, `ix`, etc.) objects on + DataFrames, resulting in refcycles that can lead to unexpectedly long-lived + DataFrames. This function attempts to clear those cycles by deleting the + cached indexers from the frame. Parameters ---------- df : pd.DataFrame """ - for attr in ('_loc', '_iloc'): + for attr in _INDEXER_NAMES: try: delattr(df, attr) except AttributeError: