Skip to content

Commit e8a2287

Browse files
authored
BUG: read_csv/table raising when sys.setprofile used (#41105)
1 parent 9773ae2 commit e8a2287

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,7 @@ I/O
832832
- Bug in :func:`read_excel` raising ``AttributeError`` with ``MultiIndex`` header followed by two empty rows and no index, and bug affecting :func:`read_excel`, :func:`read_csv`, :func:`read_table`, :func:`read_fwf`, and :func:`read_clipboard` where one blank row after a ``MultiIndex`` header with no index would be dropped (:issue:`40442`)
833833
- Bug in :meth:`DataFrame.to_string` misplacing the truncation column when ``index=False`` (:issue:`40907`)
834834
- Bug in :func:`read_orc` always raising ``AttributeError`` (:issue:`40918`)
835+
- Bug in :func:`read_csv` and :func:`read_table` misinterpreting arguments when ``sys.setprofile`` had been previously called (:issue:`41069`)
835836
- Bug in the conversion from pyarrow to pandas (e.g. for reading Parquet) with nullable dtypes and a pyarrow array whose data buffer size is not a multiple of dtype size (:issue:`40896`)
836837

837838
Period

pandas/io/parsers/readers.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,8 @@ def read_csv(
540540
float_precision=None,
541541
storage_options: StorageOptions = None,
542542
):
543-
kwds = locals()
543+
# locals() should never be modified
544+
kwds = locals().copy()
544545
del kwds["filepath_or_buffer"]
545546
del kwds["sep"]
546547

@@ -620,7 +621,8 @@ def read_table(
620621
memory_map=False,
621622
float_precision=None,
622623
):
623-
kwds = locals()
624+
# locals() should never be modified
625+
kwds = locals().copy()
624626
del kwds["filepath_or_buffer"]
625627
del kwds["sep"]
626628

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

+15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from io import StringIO
88
import os
99
from pathlib import Path
10+
import sys
1011

1112
import numpy as np
1213
import pytest
@@ -631,6 +632,20 @@ def test_read_table_equivalency_to_read_csv(all_parsers):
631632
tm.assert_frame_equal(result, expected)
632633

633634

635+
@pytest.mark.parametrize("read_func", ["read_csv", "read_table"])
636+
def test_read_csv_and_table_sys_setprofile(all_parsers, read_func):
637+
# GH#41069
638+
parser = all_parsers
639+
data = "a b\n0 1"
640+
641+
sys.setprofile(lambda *a, **k: None)
642+
result = getattr(parser, read_func)(StringIO(data))
643+
sys.setprofile(None)
644+
645+
expected = DataFrame({"a b": ["0 1"]})
646+
tm.assert_frame_equal(result, expected)
647+
648+
634649
def test_first_row_bom(all_parsers):
635650
# see gh-26545
636651
parser = all_parsers

0 commit comments

Comments
 (0)