Skip to content

Commit 486712e

Browse files
matthaxalanbato
authored andcommitted
BUG: Fix TypeError caused by GH13374 (pandas-dev#17465)
1 parent 836c4f2 commit 486712e

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ I/O
411411
- Bug in :func:`read_csv` when called with a single-element list ``header`` would return a ``DataFrame`` of all NaN values (:issue:`7757`)
412412
- Bug in :func:`read_stata` where value labels could not be read when using an iterator (:issue:`16923`)
413413
- Bug in :func:`read_html` where import check fails when run in multiple threads (:issue:`16928`)
414+
- Bug in :func:`read_csv` where automatic delimiter detection caused a ``TypeError`` to be thrown when a bad line was encountered rather than the correct error message (:issue:`13374`)
414415

415416
Plotting
416417
^^^^^^^^

pandas/io/parsers.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -2836,7 +2836,9 @@ def _rows_to_cols(self, content):
28362836
for row_num, actual_len in bad_lines:
28372837
msg = ('Expected %d fields in line %d, saw %d' %
28382838
(col_len, row_num + 1, actual_len))
2839-
if len(self.delimiter) > 1 and self.quoting != csv.QUOTE_NONE:
2839+
if (self.delimiter and
2840+
len(self.delimiter) > 1 and
2841+
self.quoting != csv.QUOTE_NONE):
28402842
# see gh-13374
28412843
reason = ('Error could possibly be due to quotes being '
28422844
'ignored when a multi-char delimiter is used.')

pandas/tests/io/parser/python_parser_only.py

+19
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,25 @@ def test_multi_char_sep_quotes(self):
218218
self.read_csv(StringIO(data), sep=',,',
219219
quoting=csv.QUOTE_NONE)
220220

221+
def test_none_delimiter(self):
222+
# see gh-13374 and gh-17465
223+
224+
data = "a,b,c\n0,1,2\n3,4,5,6\n7,8,9"
225+
expected = DataFrame({'a': [0, 7],
226+
'b': [1, 8],
227+
'c': [2, 9]})
228+
229+
# We expect the third line in the data to be
230+
# skipped because it is malformed,
231+
# but we do not expect any errors to occur.
232+
result = self.read_csv(StringIO(data), header=0,
233+
sep=None,
234+
error_bad_lines=False,
235+
warn_bad_lines=True,
236+
engine='python',
237+
tupleize_cols=True)
238+
tm.assert_frame_equal(result, expected)
239+
221240
def test_skipfooter_bad_row(self):
222241
# see gh-13879
223242
# see gh-15910

0 commit comments

Comments
 (0)