Skip to content

to_csv does not fail with specified columns not in dataframe #26613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
amcpherson opened this issue Jun 2, 2019 · 3 comments
Closed

to_csv does not fail with specified columns not in dataframe #26613

amcpherson opened this issue Jun 2, 2019 · 3 comments

Comments

@amcpherson
Copy link
Contributor

Code Sample, a copy-pastable example if possible

In [4]: data = pd.DataFrame({'a': [1, 2, 3]})

In [5]: data.to_csv('test.csv', columns=['a', 'x'])
/Users/amcphers/Projects/scgenome/venv/lib/python3.7/site-packages/pandas/core/indexing.py:1494: FutureWarning:
Passing list-likes to .loc or [] with any missing label will raise
KeyError in the future, you can use .reindex() as an alternative.

See the documentation here:
https://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate-loc-reindex-listlike
  return self._getitem_tuple(key)
...
$ head test.csv
,a,x
0,1,
1,2,
2,3,

Problem description

The documentation specified that the columns kwarg is "Columns to write." If there is no matching column to write this would seem to warent an error of some kind. Instead an unrelated warning is printed.

Expected Output

An exception should be thrown.

Output of pd.show_versions()

In [6]: pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.7.3.final.0
python-bits: 64
OS: Darwin
OS-release: 16.7.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_CA.UTF-8
LOCALE: en_CA.UTF-8

pandas: 0.24.2
pytest: None
pip: 19.1
setuptools: 41.0.1
Cython: 0.29
numpy: 1.16.2
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 5.8.0
sphinx: None
patsy: None
dateutil: 2.7.5
pytz: 2018.7
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.3
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml.etree: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

@WillAyd
Copy link
Member

WillAyd commented Jun 2, 2019

The warning isn't technically unrelated - behind the scenes columns argument is getting passed to .loc, which now allows missing labels but in the future will raise a KeyError. This is xref #15747

Until that happens you could still get the desired outcome if selected the columns from the data frame directly

>>> data[['a', 'x']].to_csv()
KeyError: "['x'] not in index"

@WillAyd
Copy link
Member

WillAyd commented Jun 2, 2019

Closing as I don't think anything needs to change with to_csv for desired outcome here; will be a byproduct of a future change as directed in the FutureWarning

@WillAyd WillAyd closed this as completed Jun 2, 2019
@smcinerney
Copy link

@WillAyd : I don't understand, on 1.0.x this still gives the very cryptic exception rather than the clear error message stating the wrong field. Are you saying the error message will be implemented, if so which milestone commitment? Can you add the commitment, or else reopen this? It is a real issue, until it's fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants