Skip to content

Tilde expansion for paths (~/...) inconsistent between read_csv and to_csv #11438

Closed
@aechase

Description

@aechase

When specifying a path in DataFrame.read_csv(), pandas has no problem with the tilde expansion:

In[1]:
    import pandas as pd
    test = pd.read_csv('~/Documents/test.csv', index_col=0)
    test.head()

Out[1]:
        Foo Bar
    0   1   2
    1   3   4
    2   5   6
    3   7   8
    4   9   10

But DataFrame.to_csv() doesn't understand this:

In[2]:
    test.to_csv('~/Documents/test1.csv')

Out[2]:
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-4-ef1160d6e930> in <module>()
----> 1 test.to_csv("~/Documents/test1.csv")

//anaconda/lib/python3.4/site-packages/pandas/core/frame.py in to_csv(self, path_or_buf, sep, na_rep, float_format, columns, header, index, index_label, mode, encoding, quoting, quotechar, line_terminator, chunksize, tupleize_cols, date_format, doublequote, escapechar, decimal, **kwds)
   1287                                      escapechar=escapechar,
   1288                                      decimal=decimal)
-> 1289         formatter.save()
   1290 
   1291         if path_or_buf is None:

//anaconda/lib/python3.4/site-packages/pandas/core/format.py in save(self)
   1467         else:
   1468             f = com._get_handle(self.path_or_buf, self.mode,
-> 1469                                 encoding=self.encoding)
   1470             close = True
   1471 

//anaconda/lib/python3.4/site-packages/pandas/core/common.py in _get_handle(path, mode, encoding, compression)
   2865                 f = open(path, mode, encoding=encoding)
   2866             else:
-> 2867                 f = open(path, mode, errors='replace')
   2868         else:
   2869             f = open(path, mode)

FileNotFoundError: [Errno 2] No such file or directory: '~/Documents/test1.csv'

Instead, it needs the file name alone, or the full path: test.to_csv('/Users/alexchase/Documents/test1.csv').

INSTALLED VERSIONS
------------------
commit: None
python: 3.4.3.final.0
python-bits: 64
OS: Darwin
OS-release: 15.0.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8

pandas: 0.17.0
nose: 1.3.7
pip: 7.1.2
setuptools: 18.4
Cython: 0.22.1
numpy: 1.10.1
scipy: 0.15.1
statsmodels: 0.6.1
IPython: 4.0.0
sphinx: 1.3.1
patsy: 0.3.0
dateutil: 2.4.2
pytz: 2015.6
blosc: None
bottleneck: 1.0.0
tables: 3.2.0
numexpr: 2.4.3
matplotlib: 1.4.3
openpyxl: 1.8.5
xlrd: 0.9.3
xlwt: 1.0.0
xlsxwriter: 0.7.3
lxml: 3.4.4
bs4: 4.3.2
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.5
pymysql: None
psycopg2: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions