Skip to content

ENH: add destructor to HDFStore #61169

@overjoy

Description

@overjoy

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

When using pd.HDFStore methods directly the store often stays open. Especially in interactive context. That can cause some permission errors:

In [1]: pd.Series([1,2,3]).to_hdf('foo.h5', 'x')

In [2]: pd.Series([1,2,3]).to_hdf('foo.h5', 'y')

In [3]: pd.HDFStore('foo.h5', 'r').keys()
Out[3]: ['/x', '/y']

In [4]: pd.Series([1,2,3]).to_hdf('foo.h5', 'z')
ValueError: The file 'foo.h5' is already opened, but in read-only mode.  Please close it before reopening in append mode.

In [5]: os.rename('foo.h5', 'bar.h5')
On Windows:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'foo.h5' -> 'bar.h5' 

Feature Description

Adding destructor solves this

pd.io.pytables.HDFStore.__del__ = lambda self: self.close()

Alternative Solutions

.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions