Skip to content

Commit bd3e37f

Browse files
Backport PR #36560: [BUG]: Fix regression in read_table with delim_whitespace=True (#36661)
Co-authored-by: patrick <[email protected]>
1 parent f953112 commit bd3e37f

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

doc/source/whatsnew/v1.1.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Fixed regressions
4040
- Fixed regression in :meth:`DataFrame.apply` with ``raw=True`` and user-function returning string (:issue:`35940`)
4141
- Fixed regression when setting empty :class:`DataFrame` column to a :class:`Series` in preserving name of index in frame (:issue:`36527`)
4242
- Fixed regression in :class:`Period` incorrect value for ordinal over the maximum timestamp (:issue:`36430`)
43+
- Fixed regression in :func:`read_table` raised ``ValueError`` when ``delim_whitespace`` was set to ``True`` (:issue:`35958`)
4344
- Fixed regression in :meth:`Series.dt.normalize` when normalizing pre-epoch dates the result was shifted one day (:issue:`36294`)
4445

4546
.. ---------------------------------------------------------------------------

pandas/io/parsers.py

+10
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,16 @@ def read_table(
752752
memory_map=False,
753753
float_precision=None,
754754
):
755+
# TODO: validation duplicated in read_csv
756+
if delim_whitespace and (delimiter is not None or sep != "\t"):
757+
raise ValueError(
758+
"Specified a delimiter with both sep and "
759+
"delim_whitespace=True; you can only specify one."
760+
)
761+
if delim_whitespace:
762+
# In this case sep is not used so we set it to the read_csv
763+
# default to avoid a ValueError
764+
sep = ","
755765
return read_csv(**locals())
756766

757767

pandas/tests/io/parser/test_common.py

+21
Original file line numberDiff line numberDiff line change
@@ -2191,3 +2191,24 @@ def test_read_csv_with_use_inf_as_na(all_parsers):
21912191
result = parser.read_csv(StringIO(data), header=None)
21922192
expected = DataFrame([1.0, np.nan, 3.0])
21932193
tm.assert_frame_equal(result, expected)
2194+
2195+
2196+
def test_read_table_delim_whitespace_default_sep(all_parsers):
2197+
# GH: 35958
2198+
f = StringIO("a b c\n1 -2 -3\n4 5 6")
2199+
parser = all_parsers
2200+
result = parser.read_table(f, delim_whitespace=True)
2201+
expected = DataFrame({"a": [1, 4], "b": [-2, 5], "c": [-3, 6]})
2202+
tm.assert_frame_equal(result, expected)
2203+
2204+
2205+
def test_read_table_delim_whitespace_non_default_sep(all_parsers):
2206+
# GH: 35958
2207+
f = StringIO("a b c\n1 -2 -3\n4 5 6")
2208+
parser = all_parsers
2209+
msg = (
2210+
"Specified a delimiter with both sep and "
2211+
"delim_whitespace=True; you can only specify one."
2212+
)
2213+
with pytest.raises(ValueError, match=msg):
2214+
parser.read_table(f, delim_whitespace=True, sep=",")

0 commit comments

Comments
 (0)