Skip to content

Commit def58c9

Browse files
committed
Merge pull request pandas-dev#9128 from hsperr/expanduser
ENH: Expanduser in to_file methods GH9066
2 parents 92d7cf7 + f00010b commit def58c9

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

doc/source/whatsnew/v0.16.0.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ Enhancements
5656

5757
.. _whatsnew_0160.enhancements:
5858

59+
- Paths beginning with ~ will now be expanded to begin with the user's home directory (:issue:`9066`)
60+
5961
Performance
6062
~~~~~~~~~~~
6163

pandas/io/common.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
"""Common IO api utilities"""
22

33
import sys
4+
import os
45
import zipfile
56
from contextlib import contextmanager, closing
67

7-
from pandas.compat import StringIO
8+
from pandas.compat import StringIO, string_types
89
from pandas import compat
910

1011

@@ -99,6 +100,24 @@ def maybe_read_encoded_stream(reader, encoding=None):
99100
return reader, encoding
100101

101102

103+
def _expand_user(filepath_or_buffer):
104+
"""Return the argument with an initial component of ~ or ~user
105+
replaced by that user's home directory.
106+
107+
Parameters
108+
----------
109+
filepath_or_buffer : object to be converted if possible
110+
111+
Returns
112+
-------
113+
expanded_filepath_or_buffer : an expanded filepath or the
114+
input if not expandable
115+
"""
116+
if isinstance(filepath_or_buffer, string_types):
117+
return os.path.expanduser(filepath_or_buffer)
118+
return filepath_or_buffer
119+
120+
102121
def get_filepath_or_buffer(filepath_or_buffer, encoding=None):
103122
"""
104123
If the filepath_or_buffer is a url, translate and return the buffer
@@ -138,7 +157,8 @@ def get_filepath_or_buffer(filepath_or_buffer, encoding=None):
138157
filepath_or_buffer = StringIO(k.get_contents_as_string())
139158
return filepath_or_buffer, None
140159

141-
return filepath_or_buffer, None
160+
161+
return _expand_user(filepath_or_buffer), None
142162

143163

144164
def file_path_to_url(path):

pandas/io/tests/test_common.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Tests for the pandas.io.common functionalities
3+
"""
4+
from pandas.compat import StringIO
5+
import os
6+
7+
import pandas.util.testing as tm
8+
9+
from pandas.io import common
10+
11+
12+
class TestCommonIOCapabilities(tm.TestCase):
13+
14+
def test_expand_user(self):
15+
filename = '~/sometest'
16+
expanded_name = common._expand_user(filename)
17+
18+
self.assertNotEqual(expanded_name, filename)
19+
self.assertNotIn('~', expanded_name)
20+
self.assertEqual(os.path.expanduser(filename), expanded_name)
21+
22+
def test_expand_user_normal_path(self):
23+
filename = '/somefolder/sometest'
24+
expanded_name = common._expand_user(filename)
25+
26+
self.assertEqual(expanded_name, filename)
27+
self.assertNotIn('~', expanded_name)
28+
self.assertEqual(os.path.expanduser(filename), expanded_name)
29+
30+
def test_get_filepath_or_buffer_with_path(self):
31+
filename = '~/sometest'
32+
filepath_or_buffer, _ = common.get_filepath_or_buffer(filename)
33+
self.assertNotEqual(filepath_or_buffer, filename)
34+
self.assertNotIn('~', filepath_or_buffer)
35+
self.assertEqual(os.path.expanduser(filename), filepath_or_buffer)
36+
37+
def test_get_filepath_or_buffer_with_buffer(self):
38+
input_buffer = StringIO()
39+
filepath_or_buffer, _ = common.get_filepath_or_buffer(input_buffer)
40+
self.assertEqual(filepath_or_buffer, input_buffer)

0 commit comments

Comments
 (0)