Skip to content

Commit

Permalink
BUG: Fix uninformative error on bad CSV engine name
Browse files Browse the repository at this point in the history
Previously had an UnboundLocalError - no fun!
  • Loading branch information
jtratner committed May 26, 2017
1 parent e81f3cc commit 26a3300
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doc/source/whatsnew/v0.20.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ Bug Fixes

- Bug in using ``pathlib.Path`` or ``py.path.local`` objects with io functions (:issue:`16291`)
- Bug in ``DataFrame.update()`` with ``overwrite=False`` and ``NaN values`` (:issue:`15593`)
- Passing an invalid engine to :func:`read_csv` now raises an informative
ValueError rather than UnboundLocalError. (:issue:`16511`)



Conversion
Expand Down
3 changes: 3 additions & 0 deletions pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,9 @@ def _make_engine(self, engine='c'):
klass = PythonParser
elif engine == 'python-fwf':
klass = FixedWidthFieldParser
else:
raise ValueError('Unknown engine: %r (valid are "c", "python",'
' or "python-fwf")' % engine)
self._engine = klass(self.f, **self.options)

def _failover_to_python(self):
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/io/parser/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import os

import pytest

import pandas.util.testing as tm

from pandas import read_csv, read_table
Expand Down Expand Up @@ -99,3 +101,13 @@ def read_table(self, *args, **kwds):
kwds = kwds.copy()
kwds['engine'] = self.engine
return read_table(*args, **kwds)


class TestParameterValidation(object):
def test_unknown_engine(self):
with tm.ensure_clean() as path:
df = tm.makeDataFrame()
df.to_csv(path)
with pytest.raises(ValueError) as exc_info:
read_csv(path, engine='pyt')
exc_info.match('Unknown engine')

0 comments on commit 26a3300

Please sign in to comment.