Skip to content

Commit baf95e9

Browse files
committed
ENH: add __fspath__ to pandas own file-like objects
- HDFStore - ExcelFile
1 parent 14c9e5f commit baf95e9

File tree

5 files changed

+25
-2
lines changed

5 files changed

+25
-2
lines changed

doc/source/whatsnew/v0.21.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ New features
2121
~~~~~~~~~~~~
2222

2323
- Support for `PEP 519 -- Adding a file system path protocol <https://www.python.org/dev/peps/pep-0519/>`_ on most readers and writers (:issue:`13823`)
24-
24+
- Added `__fspath__` method to :class`:pandas.HDFStore` and :class:`pandas.ExcelFile` to work properly with th file system path protocol (:issue:`13823`)
2525

2626

2727
.. _whatsnew_0210.enhancements.other:

pandas/io/excel.py

+3
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ def __init__(self, io, **kwds):
263263
raise ValueError('Must explicitly set engine if not passing in'
264264
' buffer or path for io.')
265265

266+
def __fspath__(self):
267+
return self._io
268+
266269
def parse(self, sheetname=0, header=0, skiprows=None, skip_footer=0,
267270
names=None, index_col=None, parse_cols=None, parse_dates=False,
268271
date_parser=None, na_values=None, thousands=None,

pandas/io/pytables.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ def __init__(self, path, mode=None, complevel=None, complib=None,
440440
"complib only supports {libs} compression.".format(
441441
libs=tables.filters.all_complibs))
442442

443-
self._path = path
443+
self._path = _stringify_path(path)
444444
if mode is None:
445445
mode = 'a'
446446
self._mode = mode
@@ -451,6 +451,9 @@ def __init__(self, path, mode=None, complevel=None, complib=None,
451451
self._filters = None
452452
self.open(mode=mode, **kwargs)
453453

454+
def __fspath__(self):
455+
return self._path
456+
454457
@property
455458
def root(self):
456459
""" return the root node """

pandas/tests/io/test_excel.py

+10
Original file line numberDiff line numberDiff line change
@@ -2499,3 +2499,13 @@ def custom_converter(css):
24992499
n_cells += 1
25002500

25012501
assert n_cells == (10 + 1) * (3 + 1)
2502+
2503+
2504+
@pytest.mark.skipif(sys.version_info < (3, 6), reason='requires fspath')
2505+
def test_excelfile_fspath():
2506+
with tm.ensure_clean('foo.xlsx') as path:
2507+
df = DataFrame({"A": [1, 2]})
2508+
df.to_excel(path)
2509+
xl = ExcelFile(path)
2510+
result = os.fspath(xl)
2511+
assert result == path

pandas/tests/io/test_pytables.py

+7
Original file line numberDiff line numberDiff line change
@@ -5154,6 +5154,13 @@ def test_query_compare_column_type(self):
51545154
expected = df.loc[[], :]
51555155
tm.assert_frame_equal(expected, result)
51565156

5157+
@pytest.mark.skipif(sys.version_info < (3, 6),
5158+
reason="requires fspath")
5159+
def test_hdfstore_fspath(self):
5160+
with tm.ensure_clean('foo.h5') as path:
5161+
with pd.HDFStore(path) as store:
5162+
assert os.fspath(store) == path
5163+
51575164

51585165
class TestHDFComplexValues(Base):
51595166
# GH10447

0 commit comments

Comments
 (0)