Skip to content

Commit 8b55e4f

Browse files
Backport PR #43447: REGR: SpooledTemporaryFile support in read_csv (#43488)
Co-authored-by: Torsten Wörtwein <[email protected]>
1 parent 5d6e352 commit 8b55e4f

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

doc/source/whatsnew/v1.3.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Fixed regressions
2929
- Fixed regression in :meth:`DataFrame.corr` where Kendall correlation would produce incorrect results for columns with repeated values (:issue:`43401`)
3030
- Fixed regression in :meth:`DataFrame.groupby` where aggregation on columns with object types dropped results on those columns (:issue:`42395`, :issue:`43108`)
3131
- Fixed regression in :meth:`Series.fillna` raising ``TypeError`` when filling ``float`` ``Series`` with list-like fill value having a dtype which couldn't cast lostlessly (like ``float32`` filled with ``float64``) (:issue:`43424`)
32+
- Fixed regression in :func:`read_csv` throwing an ``AttributeError`` when the file handle is an ``tempfile.SpooledTemporaryFile`` object (:issue:`43439`)
3233
-
3334

3435
.. ---------------------------------------------------------------------------

pandas/io/common.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
)
1616
import mmap
1717
import os
18+
import tempfile
1819
from typing import (
1920
IO,
2021
Any,
@@ -943,8 +944,15 @@ def _is_binary_mode(handle: FilePathOrBuffer, mode: str) -> bool:
943944
if "t" in mode or "b" in mode:
944945
return "b" in mode
945946

946-
# classes that expect string but have 'b' in mode
947-
text_classes = (codecs.StreamWriter, codecs.StreamReader, codecs.StreamReaderWriter)
947+
# exceptions
948+
text_classes = (
949+
# classes that expect string but have 'b' in mode
950+
codecs.StreamWriter,
951+
codecs.StreamReader,
952+
codecs.StreamReaderWriter,
953+
# cannot be wrapped in TextIOWrapper GH43439
954+
tempfile.SpooledTemporaryFile,
955+
)
948956
if issubclass(type(handle), text_classes):
949957
return False
950958

pandas/tests/io/parser/test_encoding.py

+13
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,16 @@ def test_encoding_memory_map(all_parsers, encoding):
237237
expected.to_csv(file, index=False, encoding=encoding)
238238
df = parser.read_csv(file, encoding=encoding, memory_map=True)
239239
tm.assert_frame_equal(df, expected)
240+
241+
242+
def test_not_readable(all_parsers):
243+
# GH43439
244+
parser = all_parsers
245+
if parser.engine in ("python", "pyarrow"):
246+
pytest.skip("SpooledTemporaryFile does only work with the c-engine")
247+
with tempfile.SpooledTemporaryFile() as handle:
248+
handle.write(b"abcd")
249+
handle.seek(0)
250+
df = parser.read_csv(handle)
251+
expected = DataFrame([], columns=["abcd"])
252+
tm.assert_frame_equal(df, expected)

0 commit comments

Comments
 (0)