Skip to content

BUG: PeriodDtype and IntervalDtype holding references to itself when object is deleted #54184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 19, 2023

Conversation

mroeschke
Copy link
Member

Fixes this issue

In [1]: import pandas as pd

In [2]: import weakref

In [3]: pdt = pd.PeriodDtype(freq="D")

In [4]: ref = weakref.ref(pdt)

In [5]: del pdt

In [6]: import gc; gc.collect()
Out[6]: 1119

In [7]: ref()
Out[7]: period[D]

In [8]: idt = pd.IntervalDtype("interval[int64, right]")

In [9]: ref2 = weakref.ref(idt)

In [10]: del idt

In [11]: import gc; gc.collect()
Out[11]: 615

In [12]: ref2()
Out[12]: interval[int64, right]
  1. For IntervalDtype the cache didn't really seem to improve performance now it's not used
  2. For PeriodDtype just caching the period freq code instead of the created object
# PR

In [2]: %timeit pd.IntervalDtype("interval[int64, right]")
11.1 µs ± 13 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

In [3]: %timeit pd.PeriodDtype(freq="D")
22.2 µs ± 135 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

# main

In [2]: idt = pd.IntervalDtype("interval[int64, right]")

In [3]: %timeit pd.IntervalDtype("interval[int64, right]")
14 µs ± 28.6 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

In [4]: pdt = pd.PeriodDtype(freq="D")

In [5]: %timeit pd.PeriodDtype(freq="D")
22.2 µs ± 182 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

@mroeschke mroeschke added Period Period data type Interval Interval data type labels Jul 18, 2023
@@ -1155,7 +1155,7 @@ class IntervalDtype(PandasExtensionDtype):

_cache_dtypes: dict[str_type, PandasExtensionDtype] = {}

def __new__(cls, subtype=None, closed: IntervalClosedType | None = None):
def __init__(self, subtype=None, closed: IntervalClosedType | None = None) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the PeriodDtype __new__ be changed too?

Copy link
Member Author

@mroeschke mroeschke Jul 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think PeriodDtype has a PeriodDtypeBase.__cinit__ that is supposed to be "private", so I think it should continue to have its own __new__

@jbrockmendel
Copy link
Member

mypy complaint, otherwise looks good

@mroeschke mroeschke added this to the 2.1 milestone Jul 19, 2023
@mroeschke mroeschke merged commit ab89aef into pandas-dev:main Jul 19, 2023
@mroeschke mroeschke deleted the bug/dtype/ref branch July 19, 2023 18:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Interval Interval data type Period Period data type
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants