Skip to content

BUG: read_csv used in file like object RawIOBase is not recognize encoding option #31596

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
merged 5 commits into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ I/O
^^^
- Bug in :meth:`read_json` where integer overflow was occuring when json contains big number strings. (:issue:`30320`)
- Bug in :meth:`DataFrame.to_json` was raising ``NotFoundError`` when ``path_or_buf`` was an S3 URI (:issue:`28375`)
- Bug in :meth:`read_csv` used in file like object ``RawIOBase`` is not recognize ``encoding`` option (:issue:`31575`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can move to 1.0.1 (same section)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved it.

-

Plotting
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 @@ -359,9 +359,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 @@ -437,7 +437,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 @@ -1868,7 +1868,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 @@ -141,6 +141,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 @@ -154,6 +155,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