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

Open
aechase opened this Issue Oct 27, 2015 · 4 comments

Comments

Projects
None yet
3 participants
Contributor

aechase commented Oct 27, 2015

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

TomAugspurger added this to the 0.17.1 milestone Oct 27, 2015

Contributor

TomAugspurger commented Oct 27, 2015

Thanks for the report. Interested in submitting a Pull Request?

Contributor

aechase commented Oct 27, 2015

Sure, I'll try to take a look this weekend.

@jreback jreback added Enhancement and removed Bug labels Oct 27, 2015

@jreback jreback modified the milestone: Next Major Release, 0.17.1 Oct 27, 2015

Contributor

aechase commented Oct 28, 2015

Many thanks!

@yarikoptic yarikoptic added a commit to neurodebian/pandas that referenced this issue Dec 3, 2015

@yarikoptic yarikoptic Merge tag 'v0.17.1' into debian
Version 0.17.1

* tag 'v0.17.1': (168 commits)
  add nbviewer link
  Revert "DOC: fix sponsor notice"
  DOC: a few touchups
  DOC: fix sponsor notice
  DOC: warnings and remove HTML
  COMPAT: compat of scalars on all platforms, xref #11638
  DOC: fix build errors/warnings
  DOC: whatsnew edits
  DOC: fix link syntax
  DOC: update release.rst / whatsnew edits
  BUG: fix col iteration in DataFrame.round, #11611
  DOC: Clarify foramtting
  BUG: #11638 return correct dtype for int and float
  BUG: #11637 fix to_csv incorrect output.
  DOC: sponsor notice
  BUG: indexing with a range , #11652
  Fix link to numexpr
  ENH: fixup tilde expansion, xref #11438
  ENH: tilde expansion for write output formatting functions, #11438
  DOC: fix up doc-string creations in generic.py
  ...
9b2e35f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment