Skip to content

REG: Restore read_csv function for some file-likes #32577

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 1 commit into from
Mar 11, 2020
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.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Fixed regressions
- Fixed regression in :meth:`pandas.core.groupby.GroupBy.agg` calling a user-provided function an extra time on an empty input (:issue:`31760`)
- Joining on :class:`DatetimeIndex` or :class:`TimedeltaIndex` will preserve ``freq`` in simple cases (:issue:`32166`)
- Fixed bug in the repr of an object-dtype :class:`Index` with bools and missing values (:issue:`32146`)
- Fixed regression in :meth:`read_csv` in which the ``encoding`` option was not recognized with certain file-like objects (:issue:`31819`)
-

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

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

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, RawIOBase, StringIO, TextIOWrapper
from io import StringIO, TextIOWrapper
import re
import sys
from textwrap import fill
Expand Down Expand Up @@ -1870,7 +1870,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, RawIOBase)):
if hasattr(src, "read") and not hasattr(src, "encoding"):
src = TextIOWrapper(src, encoding=encoding, newline="")

kwds["encoding"] = "utf-8"
Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/io/parser/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from io import BytesIO
import os
import tempfile

import numpy as np
import pytest
Expand Down Expand Up @@ -174,3 +175,25 @@ def test_encoding_temp_file(all_parsers, utf_value, encoding_fmt, pass_encoding)

result = parser.read_csv(f, encoding=encoding if pass_encoding else None)
tm.assert_frame_equal(result, expected)


def test_encoding_named_temp_file(all_parsers):
# see gh-31819
parser = all_parsers
encoding = "shift-jis"

if parser.engine == "python":
pytest.skip("NamedTemporaryFile does not work with Python engine")

title = "てすと"
data = "こむ"

expected = DataFrame({title: [data]})

with tempfile.NamedTemporaryFile() as f:
f.write(f"{title}\n{data}".encode(encoding))

f.seek(0)

result = parser.read_csv(f, encoding=encoding)
tm.assert_frame_equal(result, expected)