Feature Type
-
[x] 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
Comment From: snitish
Context managers and explicitly calling .close()
are the two recommended ways to deal with objects like HDFStore
. I don't think it is good practice to rely on the __del__
method, as it is not guaranteed to be called.
Comment From: mroeschke
Agreed with @snitish. Since HDFStore
already has APIs for closing, closing this issue