Skip to content
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
15 changes: 7 additions & 8 deletions pandas/tests/io/parser/common/test_read_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def test_null_byte_char(request, all_parsers):


@pytest.mark.filterwarnings("always::ResourceWarning")
def test_open_file(all_parsers):
def test_open_file(all_parsers, temp_file):
# GH 39024
parser = all_parsers

Expand All @@ -259,14 +259,13 @@ def test_open_file(all_parsers):
msg = "'utf-8' codec can't decode byte 0xe4"
err = ValueError

with tm.ensure_clean() as path:
file = Path(path)
file.write_bytes(b"\xe4\na\n1")
file = Path(temp_file)
file.write_bytes(b"\xe4\na\n1")

with tm.assert_produces_warning(None):
# should not trigger a ResourceWarning
with pytest.raises(err, match=msg):
parser.read_csv(file, sep=None, encoding_errors="replace")
with tm.assert_produces_warning(None):
# should not trigger a ResourceWarning
with pytest.raises(err, match=msg):
parser.read_csv(file, sep=None, encoding_errors="replace")


def test_invalid_on_bad_line(all_parsers):
Expand Down
26 changes: 14 additions & 12 deletions pandas/tests/io/parser/dtypes/test_dtypes_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
@pytest.mark.parametrize("dtype", [str, object])
@pytest.mark.parametrize("check_orig", [True, False])
@pytest.mark.usefixtures("pyarrow_xfail")
def test_dtype_all_columns(all_parsers, dtype, check_orig, using_infer_string):
def test_dtype_all_columns(
all_parsers, dtype, check_orig, using_infer_string, tmp_path
):
# see gh-3795, gh-6607
parser = all_parsers

Expand All @@ -39,20 +41,20 @@ def test_dtype_all_columns(all_parsers, dtype, check_orig, using_infer_string):
index=["1A", "1B", "1C", "1D", "1E"],
)

with tm.ensure_clean("__passing_str_as_dtype__.csv") as path:
df.to_csv(path)
path = tmp_path / "__passing_str_as_dtype__.csv"
df.to_csv(path)

result = parser.read_csv(path, dtype=dtype, index_col=0)
result = parser.read_csv(path, dtype=dtype, index_col=0)

if check_orig:
expected = df.copy()
result = result.astype(float)
elif using_infer_string and dtype is str:
expected = df.astype(str)
else:
expected = df.astype(str).astype(object)
if check_orig:
expected = df.copy()
result = result.astype(float)
elif using_infer_string and dtype is str:
expected = df.astype(str)
else:
expected = df.astype(str).astype(object)

tm.assert_frame_equal(result, expected)
tm.assert_frame_equal(result, expected)


@pytest.mark.usefixtures("pyarrow_xfail")
Expand Down
34 changes: 17 additions & 17 deletions pandas/tests/io/parser/test_c_parser_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,19 @@ def test_dtype_and_names_error(c_parser_only):
],
ids=["dt64-0", "dt64-1", "td64", f"{tm.ENDIAN}U8"],
)
def test_unsupported_dtype(c_parser_only, match, kwargs):
def test_unsupported_dtype(c_parser_only, match, kwargs, tmp_path):
parser = c_parser_only
df = DataFrame(
np.random.default_rng(2).random((5, 2)),
columns=list("AB"),
index=["1A", "1B", "1C", "1D", "1E"],
)

with tm.ensure_clean("__unsupported_dtype__.csv") as path:
df.to_csv(path)
path = tmp_path / "__unsupported_dtype__.csv"
df.to_csv(path)

with pytest.raises(TypeError, match=match):
parser.read_csv(path, index_col=0, **kwargs)
with pytest.raises(TypeError, match=match):
parser.read_csv(path, index_col=0, **kwargs)


@td.skip_if_32bit
Expand Down Expand Up @@ -563,27 +563,27 @@ def test_file_handles_mmap(c_parser_only, csv1):
assert not m.closed


def test_file_binary_mode(c_parser_only):
def test_file_binary_mode(c_parser_only, temp_file):
# see gh-23779
parser = c_parser_only
expected = DataFrame([[1, 2, 3], [4, 5, 6]])

with tm.ensure_clean() as path:
with open(path, "w", encoding="utf-8") as f:
f.write("1,2,3\n4,5,6")
path = temp_file
with open(path, "w", encoding="utf-8") as f:
f.write("1,2,3\n4,5,6")

with open(path, "rb") as f:
result = parser.read_csv(f, header=None)
tm.assert_frame_equal(result, expected)
with open(path, "rb") as f:
result = parser.read_csv(f, header=None)
tm.assert_frame_equal(result, expected)


def test_unix_style_breaks(c_parser_only):
def test_unix_style_breaks(c_parser_only, temp_file):
# GH 11020
parser = c_parser_only
with tm.ensure_clean() as path:
with open(path, "w", newline="\n", encoding="utf-8") as f:
f.write("blah\n\ncol_1,col_2,col_3\n\n")
result = parser.read_csv(path, skiprows=2, encoding="utf-8", engine="c")
path = temp_file
with open(path, "w", newline="\n", encoding="utf-8") as f:
f.write("blah\n\ncol_1,col_2,col_3\n\n")
result = parser.read_csv(path, skiprows=2, encoding="utf-8", engine="c")
expected = DataFrame(columns=["col_1", "col_2", "col_3"])
tm.assert_frame_equal(result, expected)

Expand Down
Loading