Skip to content

Commit c2c87d2

Browse files
srinivasreddyPingviinituutti
authored andcommitted
Add _expand_user to _stringify_path as we need to allow tilde - ~, to expand to full path (pandas-dev#23769)
1 parent e2981f7 commit c2c87d2

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ Other Enhancements
416416
- :class:`IntervalIndex` has gained the :attr:`~IntervalIndex.is_overlapping` attribute to indicate if the ``IntervalIndex`` contains any overlapping intervals (:issue:`23309`)
417417
- :func:`pandas.DataFrame.to_sql` has gained the ``method`` argument to control SQL insertion clause. See the :ref:`insertion method <io.sql.method>` section in the documentation. (:issue:`8953`)
418418
- :meth:`DataFrame.corrwith` now supports Spearman's rank correlation, Kendall's tau as well as callable correlation methods. (:issue:`21925`)
419+
- :meth:`DataFrame.to_json`, :meth:`DataFrame.to_csv`, :meth:`DataFrame.to_pickle`, and :meth:`DataFrame.to_XXX` etc. now support tilde(~) in path argument. (:issue:`23473`)
419420

420421
.. _whatsnew_0240.api_breaking:
421422

pandas/io/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def _stringify_path(filepath_or_buffer):
157157
return text_type(filepath_or_buffer)
158158
if _PY_PATH_INSTALLED and isinstance(filepath_or_buffer, LocalPath):
159159
return filepath_or_buffer.strpath
160-
return filepath_or_buffer
160+
return _expand_user(filepath_or_buffer)
161161

162162

163163
def is_s3_url(url):

pandas/tests/io/test_common.py

+26
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44
import mmap
55
import os
6+
import re
67

78
import pytest
89

@@ -148,6 +149,31 @@ def test_read_non_existant(self, reader, module, error_class, fn_ext):
148149
with pytest.raises(error_class):
149150
reader(path)
150151

152+
@pytest.mark.parametrize('reader, module, error_class, fn_ext', [
153+
(pd.read_csv, 'os', FileNotFoundError, 'csv'),
154+
(pd.read_fwf, 'os', FileNotFoundError, 'txt'),
155+
(pd.read_excel, 'xlrd', FileNotFoundError, 'xlsx'),
156+
(pd.read_feather, 'feather', Exception, 'feather'),
157+
(pd.read_hdf, 'tables', FileNotFoundError, 'h5'),
158+
(pd.read_stata, 'os', FileNotFoundError, 'dta'),
159+
(pd.read_sas, 'os', FileNotFoundError, 'sas7bdat'),
160+
(pd.read_json, 'os', ValueError, 'json'),
161+
(pd.read_msgpack, 'os', ValueError, 'mp'),
162+
(pd.read_pickle, 'os', FileNotFoundError, 'pickle'),
163+
])
164+
def test_read_expands_user_home_dir(self, reader, module,
165+
error_class, fn_ext, monkeypatch):
166+
pytest.importorskip(module)
167+
168+
path = os.path.join('~', 'does_not_exist.' + fn_ext)
169+
monkeypatch.setattr(icom, '_expand_user',
170+
lambda x: os.path.join('foo', x))
171+
172+
message = "".join(["foo", os.path.sep, "does_not_exist.", fn_ext])
173+
174+
with pytest.raises(error_class, message=re.escape(message)):
175+
reader(path)
176+
151177
def test_read_non_existant_read_table(self):
152178
path = os.path.join(HERE, 'data', 'does_not_exist.' + 'csv')
153179
with pytest.raises(FileNotFoundError):

0 commit comments

Comments
 (0)