Skip to content

Commit 9a4f1a7

Browse files
committed
BUG: Fix uninformative error on bad CSV engine name
Previously had an UnboundLocalError - no fun!
1 parent b0d9ee0 commit 9a4f1a7

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

doc/source/whatsnew/v0.20.2.txt

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ Bug Fixes
3939

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

4346

4447

pandas/io/parsers.py

+3
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,9 @@ def _make_engine(self, engine='c'):
969969
klass = PythonParser
970970
elif engine == 'python-fwf':
971971
klass = FixedWidthFieldParser
972+
else:
973+
raise ValueError('Unknown engine: %r (valid are "c", "python",'
974+
' or "python-fwf")' % engine)
972975
self._engine = klass(self.f, **self.options)
973976

974977
def _failover_to_python(self):

pandas/tests/io/parser/test_parsers.py

+12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import os
44

5+
import pytest
6+
57
import pandas.util.testing as tm
68

79
from pandas import read_csv, read_table
@@ -99,3 +101,13 @@ def read_table(self, *args, **kwds):
99101
kwds = kwds.copy()
100102
kwds['engine'] = self.engine
101103
return read_table(*args, **kwds)
104+
105+
106+
class TestParameterValidation(object):
107+
def test_unknown_engine(self):
108+
with tm.ensure_clean() as path:
109+
df = tm.makeDataFrame()
110+
df.to_csv(path)
111+
with pytest.raises(ValueError) as exc_info:
112+
read_csv(path, engine='pyt')
113+
exc_info.match('Unknown engine')

0 commit comments

Comments
 (0)