Skip to content

Commit 66d33b2

Browse files
authored
BUG: read_csv not raising when \n as sep (#44899)
1 parent a769e38 commit 66d33b2

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ I/O
749749
- Bug in :func:`read_csv` raising ``ValueError`` when names was longer than header but equal to data rows for ``engine="python"`` (:issue:`38453`)
750750
- Bug in :class:`ExcelWriter`, where ``engine_kwargs`` were not passed through to all engines (:issue:`43442`)
751751
- Bug in :func:`read_csv` raising ``ValueError`` when ``parse_dates`` was used with ``MultiIndex`` columns (:issue:`8991`)
752+
- Bug in :func:`read_csv` not raising an ``ValueError`` when ``\n`` was specified as ``delimiter`` or ``sep`` which conflicts with ``lineterminator`` (:issue:`43528`)
752753
- Bug in :func:`read_csv` converting columns to numeric after date parsing failed (:issue:`11019`)
753754
- Bug in :func:`read_csv` not replacing ``NaN`` values with ``np.nan`` before attempting date conversion (:issue:`26203`)
754755
- Bug in :func:`read_csv` raising ``AttributeError`` when attempting to read a .csv file and infer index column dtype from an nullable integer type (:issue:`44079`)

pandas/io/parsers/readers.py

+7
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,13 @@ def _refine_defaults_read(
14601460
"delim_whitespace=True; you can only specify one."
14611461
)
14621462

1463+
if delimiter == "\n":
1464+
raise ValueError(
1465+
r"Specified \n as separator or delimiter. This forces the python engine "
1466+
"which does not accept a line terminator. Hence it is not allowed to use "
1467+
"the line terminator as separator.",
1468+
)
1469+
14631470
if delimiter is lib.no_default:
14641471
# assign default separator value
14651472
kwds["delimiter"] = delim_default

pandas/tests/io/parser/common/test_common_basic.py

+16
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,22 @@ def test_read_csv_delimiter_and_sep_no_default(all_parsers):
788788
parser.read_csv(f, sep=" ", delimiter=".")
789789

790790

791+
@pytest.mark.parametrize("kwargs", [{"delimiter": "\n"}, {"sep": "\n"}])
792+
def test_read_csv_line_break_as_separator(kwargs, all_parsers):
793+
# GH#43528
794+
parser = all_parsers
795+
data = """a,b,c
796+
1,2,3
797+
"""
798+
msg = (
799+
r"Specified \\n as separator or delimiter. This forces the python engine "
800+
r"which does not accept a line terminator. Hence it is not allowed to use "
801+
r"the line terminator as separator."
802+
)
803+
with pytest.raises(ValueError, match=msg):
804+
parser.read_csv(StringIO(data), **kwargs)
805+
806+
791807
def test_read_csv_posargs_deprecation(all_parsers):
792808
# GH 41485
793809
f = StringIO("a,b\n1,2")

0 commit comments

Comments
 (0)