Skip to content

Backport PR #31596 on branch 1.0.x (BUG: read_csv used in file like object RawIOBase is not recognize encoding option) #31698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Fixed regressions
- Fixed regression in :meth:`qcut` when passed a nullable integer. (:issue:`31389`)
- Fixed regression in assigning to a :class:`Series` using a nullable integer dtype (:issue:`31446`)
- Fixed performance regression when indexing a ``DataFrame`` or ``Series`` with a :class:`MultiIndex` for the index using a list of labels (:issue:`31648`)
- Fixed regression in :meth:`read_csv` used in file like object ``RawIOBase`` is not recognize ``encoding`` option (:issue:`31575`)

.. ---------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/parsers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ cdef class TextReader:
raise ValueError(f'Unrecognized compression type: '
f'{self.compression}')

if self.encoding and isinstance(source, io.BufferedIOBase):
if self.encoding and isinstance(source, (io.BufferedIOBase, io.RawIOBase)):
source = io.TextIOWrapper(
source, self.encoding.decode('utf-8'), newline='')

Expand Down
8 changes: 4 additions & 4 deletions pandas/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import bz2
from collections import abc
import gzip
from io import BufferedIOBase, BytesIO
from io import BufferedIOBase, BytesIO, RawIOBase
import mmap
import os
import pathlib
Expand Down Expand Up @@ -361,9 +361,9 @@ def get_handle(
try:
from s3fs import S3File

need_text_wrapping = (BufferedIOBase, S3File)
need_text_wrapping = (BufferedIOBase, RawIOBase, S3File)
except ImportError:
need_text_wrapping = BufferedIOBase # type: ignore
need_text_wrapping = (BufferedIOBase, RawIOBase) # type: ignore

handles: List[IO] = list()
f = path_or_buf
Expand Down Expand Up @@ -439,7 +439,7 @@ def get_handle(
from io import TextIOWrapper

g = TextIOWrapper(f, encoding=encoding, newline="")
if not isinstance(f, BufferedIOBase):
if not isinstance(f, (BufferedIOBase, RawIOBase)):
handles.append(g)
f = g

Expand Down
4 changes: 2 additions & 2 deletions pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from collections import abc, defaultdict
import csv
import datetime
from io import BufferedIOBase, StringIO, TextIOWrapper
from io import BufferedIOBase, RawIOBase, StringIO, TextIOWrapper
import re
import sys
from textwrap import fill
Expand Down Expand Up @@ -1876,7 +1876,7 @@ def __init__(self, src, **kwds):

# Handle the file object with universal line mode enabled.
# We will handle the newline character ourselves later on.
if isinstance(src, BufferedIOBase):
if isinstance(src, (BufferedIOBase, RawIOBase)):
src = TextIOWrapper(src, encoding=encoding, newline="")

kwds["encoding"] = "utf-8"
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/io/parser/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def test_read_csv_utf_aliases(all_parsers, utf_value, encoding_fmt):
)
def test_binary_mode_file_buffers(all_parsers, csv_dir_path, fname, encoding):
# gh-23779: Python csv engine shouldn't error on files opened in binary.
# gh-31575: Python csv engine shouldn't error on files opened in raw binary.
parser = all_parsers

fpath = os.path.join(csv_dir_path, fname)
Expand All @@ -155,6 +156,10 @@ def test_binary_mode_file_buffers(all_parsers, csv_dir_path, fname, encoding):
result = parser.read_csv(fb, encoding=encoding)
tm.assert_frame_equal(expected, result)

with open(fpath, mode="rb", buffering=0) as fb:
result = parser.read_csv(fb, encoding=encoding)
tm.assert_frame_equal(expected, result)


@pytest.mark.parametrize("pass_encoding", [True, False])
def test_encoding_temp_file(all_parsers, utf_value, encoding_fmt, pass_encoding):
Expand Down