Skip to content

Commit 62bed0e

Browse files
quintusdiasjreback
authored andcommitted
COMPAT: Add Pathlib, py.path support for read_hdf
Closes #11773 Author: John Evans <[email protected]> Closes #12930 from quintusdias/issue11773 and squashes the following commits: dcee282 [John Evans] COMPAT: Add Pathlib, py.path support for read_hdf, to_hdf
1 parent f637aa3 commit 62bed0e

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

doc/source/whatsnew/v0.18.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Other enhancements
3232
- The ``.tz_localize()`` method of ``DatetimeIndex`` and ``Timestamp`` has gained the ``errors`` keyword, so you can potentially coerce nonexistent timestamps to ``NaT``. The default behaviour remains to raising a ``NonExistentTimeError`` (:issue:`13057`)
3333

3434
- ``Index`` now supports ``.str.extractall()`` which returns ``DataFrame``, see :ref:`Extract all matches in each subject (extractall) <text.extractall>` (:issue:`10008`, :issue:`13156`)
35+
- ``.to_hdf/read_hdf()`` now accept path objects (e.g. ``pathlib.Path``, ``py.path.local``) for the file path (:issue:`11773`)
3536

3637
.. ipython:: python
3738

pandas/io/pytables.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
import os
1414

1515
import numpy as np
16+
1617
import pandas as pd
1718
from pandas import (Series, DataFrame, Panel, Panel4D, Index,
1819
MultiIndex, Int64Index)
1920
from pandas.core import config
21+
from pandas.io.common import _stringify_path
2022
from pandas.sparse.api import SparseSeries, SparseDataFrame, SparsePanel
2123
from pandas.sparse.array import BlockIndex, IntIndex
2224
from pandas.tseries.api import PeriodIndex, DatetimeIndex
@@ -254,6 +256,7 @@ def to_hdf(path_or_buf, key, value, mode=None, complevel=None, complib=None,
254256
else:
255257
f = lambda store: store.put(key, value, **kwargs)
256258

259+
path_or_buf = _stringify_path(path_or_buf)
257260
if isinstance(path_or_buf, string_types):
258261
with HDFStore(path_or_buf, mode=mode, complevel=complevel,
259262
complib=complib) as store:
@@ -270,7 +273,11 @@ def read_hdf(path_or_buf, key=None, **kwargs):
270273
271274
Parameters
272275
----------
273-
path_or_buf : path (string), or buffer to read from
276+
path_or_buf : path (string), buffer, or path object (pathlib.Path or
277+
py._path.local.LocalPath) to read from
278+
279+
.. versionadded:: 0.18.2 support for pathlib, py.path.
280+
274281
key : group identifier in the store. Can be omitted a HDF file contains
275282
a single pandas object.
276283
where : list of Term (or convertable) objects, optional
@@ -293,6 +300,7 @@ def read_hdf(path_or_buf, key=None, **kwargs):
293300
if 'where' in kwargs:
294301
kwargs['where'] = _ensure_term(kwargs['where'], scope_level=1)
295302

303+
path_or_buf = _stringify_path(path_or_buf)
296304
if isinstance(path_or_buf, string_types):
297305

298306
try:
@@ -316,6 +324,7 @@ def read_hdf(path_or_buf, key=None, **kwargs):
316324

317325
store = path_or_buf
318326
auto_close = False
327+
319328
else:
320329
raise NotImplementedError('Support for generic buffers has not been '
321330
'implemented.')

pandas/io/tests/test_pytables.py

+36
Original file line numberDiff line numberDiff line change
@@ -4836,6 +4836,42 @@ def test_read_nokey(self):
48364836
df.to_hdf(path, 'df2', mode='a')
48374837
self.assertRaises(ValueError, read_hdf, path)
48384838

4839+
def test_read_from_pathlib_path(self):
4840+
4841+
# GH11773
4842+
tm._skip_if_no_pathlib()
4843+
4844+
from pathlib import Path
4845+
4846+
expected = DataFrame(np.random.rand(4, 5),
4847+
index=list('abcd'),
4848+
columns=list('ABCDE'))
4849+
with ensure_clean_path(self.path) as filename:
4850+
path_obj = Path(filename)
4851+
4852+
expected.to_hdf(path_obj, 'df', mode='a')
4853+
actual = read_hdf(path_obj, 'df')
4854+
4855+
tm.assert_frame_equal(expected, actual)
4856+
4857+
def test_read_from_py_localpath(self):
4858+
4859+
# GH11773
4860+
tm._skip_if_no_localpath()
4861+
4862+
from py.path import local as LocalPath
4863+
4864+
expected = DataFrame(np.random.rand(4, 5),
4865+
index=list('abcd'),
4866+
columns=list('ABCDE'))
4867+
with ensure_clean_path(self.path) as filename:
4868+
path_obj = LocalPath(filename)
4869+
4870+
expected.to_hdf(path_obj, 'df', mode='a')
4871+
actual = read_hdf(path_obj, 'df')
4872+
4873+
tm.assert_frame_equal(expected, actual)
4874+
48394875

48404876
class TestHDFComplexValues(Base):
48414877
# GH10447

0 commit comments

Comments
 (0)