Skip to content

BUG: use TypeError (not OSError) when read_csv expects file path name or file-like object #43443

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

Closed
wants to merge 1 commit into from
Closed
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.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ I/O
- Column headers are dropped when constructing a :class:`DataFrame` from a sqlalchemy's ``Row`` object (:issue:`40682`)
- Bug in unpickling a :class:`Index` with object dtype incorrectly inferring numeric dtypes (:issue:`43188`)
- Bug in :func:`read_csv` where reading multi-header input with unequal lengths incorrectly raising uncontrolled ``IndexError`` (:issue:`43102`)
- Bug in :func:`read_csv`, changed exception class when expecting a file path name or file-like object from ``OSError`` to ``TypeError`` (:issue:`43443`)

Period
^^^^^^
Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/parsers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -607,8 +607,8 @@ cdef class TextReader:
void *ptr

if not hasattr(source, "read"):
raise IOError(f'Expected file path name or file-like object, '
f'got {type(source)} type')
raise TypeError('Expected file path name or file-like object, '
f'got {type(source)} type')

ptr = new_rd_source(source)
self.parser.source = ptr
Expand Down
4 changes: 4 additions & 0 deletions pandas/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,10 @@ def get_handle(
isinstance(ioargs.filepath_or_buffer, str) or ioargs.should_close
)

if not hasattr(handle, "read"):
raise TypeError('Expected file path name or file-like object, '
f'got {type(ioargs.filepath_or_buffer)} type')

handles.reverse() # close the most recently added buffer first
if ioargs.should_close:
assert not isinstance(ioargs.filepath_or_buffer, str)
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/io/parser/common/test_common_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,14 @@ def test_raise_on_sep_with_delim_whitespace(all_parsers):
parser.read_csv(StringIO(data), sep=r"\s", delim_whitespace=True)


def test_read_filepath_or_buffer(all_parsers):
# see gh-43443
parser = all_parsers

with pytest.raises(TypeError, match="Expected file path name or file-lik"):
parser.read_csv(filepath_or_buffer=b'input')


@xfail_pyarrow
@pytest.mark.parametrize("delim_whitespace", [True, False])
def test_single_char_leading_whitespace(all_parsers, delim_whitespace):
Expand Down