Skip to content

Commit 6ba36e7

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

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

doc/source/whatsnew/v0.21.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ Performance Improvements
8383
Bug Fixes
8484
~~~~~~~~~
8585

86+
- Passing an invalid engine to `read_csv` now raises an informative ValueError rather than UnboundLocalError. (:issue:`16511`)
87+
8688
Conversion
8789
^^^^^^^^^^
8890

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

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# -*- coding: utf-8 -*-
22

33
import os
4+
import tempfile
5+
6+
import pytest
47

58
import pandas.util.testing as tm
69

@@ -99,3 +102,14 @@ def read_table(self, *args, **kwds):
99102
kwds = kwds.copy()
100103
kwds['engine'] = self.engine
101104
return read_table(*args, **kwds)
105+
106+
107+
class TestParameterValidation(object):
108+
def test_unknown_engine(self):
109+
with tempfile.NamedTemporaryFile() as fp:
110+
df = tm.makeDataFrame()
111+
df.to_csv(fp.name)
112+
fp.flush()
113+
with pytest.raises(ValueError) as exc_info:
114+
read_csv(fp.name, engine='pyt')
115+
exc_info.match('Unknown engine')

0 commit comments

Comments
 (0)