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
10 changes: 5 additions & 5 deletions pandas/tests/io/parser/test_parse_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def test_date_col_as_index_col(all_parsers):


@xfail_pyarrow
def test_nat_parse(all_parsers):
def test_nat_parse(all_parsers, temp_file):
# see gh-3062
parser = all_parsers
df = DataFrame(
Expand All @@ -97,11 +97,11 @@ def test_nat_parse(all_parsers):
)
df.iloc[3:6, :] = np.nan

with tm.ensure_clean("__nat_parse_.csv") as path:
df.to_csv(path)
path = temp_file
df.to_csv(path)

result = parser.read_csv(path, index_col=0, parse_dates=["B"])
tm.assert_frame_equal(result, df)
result = parser.read_csv(path, index_col=0, parse_dates=["B"])
tm.assert_frame_equal(result, df)


@skip_pyarrow
Expand Down
49 changes: 24 additions & 25 deletions pandas/tests/io/parser/test_read_fwf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
BytesIO,
StringIO,
)
from pathlib import Path

import numpy as np
import pytest
Expand Down Expand Up @@ -642,7 +641,7 @@ def test_default_delimiter():


@pytest.mark.parametrize("infer", [True, False])
def test_fwf_compression(compression_only, infer, compression_to_extension):
def test_fwf_compression(compression_only, infer, compression_to_extension, temp_file):
data = """1111111111
2222222222
3333333333""".strip()
Expand All @@ -655,17 +654,17 @@ def test_fwf_compression(compression_only, infer, compression_to_extension):

data = bytes(data, encoding="utf-8")

with tm.ensure_clean(filename="tmp." + extension) as path:
tm.write_to_compressed(compression, path, data)
path = temp_file.parent / f"tmp.{extension}"
tm.write_to_compressed(compression, path, data)

if infer is not None:
kwargs["compression"] = "infer" if infer else compression
if infer is not None:
kwargs["compression"] = "infer" if infer else compression

result = read_fwf(path, **kwargs)
tm.assert_frame_equal(result, expected)
result = read_fwf(path, **kwargs)
tm.assert_frame_equal(result, expected)


def test_binary_mode():
def test_binary_mode(temp_file):
"""
read_fwf supports opening files in binary mode.

Expand All @@ -676,31 +675,31 @@ def test_binary_mode():
df_reference = DataFrame(
[["bba", "bab", "b a"]], columns=["aaa", "aaa.1", "aaa.2"], index=[0]
)
with tm.ensure_clean() as path:
Path(path).write_text(data, encoding="utf-8")
with open(path, "rb") as file:
df = read_fwf(file)
file.seek(0)
tm.assert_frame_equal(df, df_reference)
path = temp_file
path.write_text(data, encoding="utf-8")
with open(path, "rb") as file:
df = read_fwf(file)
file.seek(0)
tm.assert_frame_equal(df, df_reference)


@pytest.mark.parametrize("memory_map", [True, False])
def test_encoding_mmap(memory_map):
def test_encoding_mmap(memory_map, temp_file):
"""
encoding should be working, even when using a memory-mapped file.

GH 23254.
"""
encoding = "iso8859_1"
with tm.ensure_clean() as path:
Path(path).write_bytes(" 1 A Ä 2\n".encode(encoding))
df = read_fwf(
path,
header=None,
widths=[2, 2, 2, 2],
encoding=encoding,
memory_map=memory_map,
)
path = temp_file
path.write_bytes(" 1 A Ä 2\n".encode(encoding))
df = read_fwf(
path,
header=None,
widths=[2, 2, 2, 2],
encoding=encoding,
memory_map=memory_map,
)
df_reference = DataFrame([[1, "A", "Ä", 2]])
tm.assert_frame_equal(df, df_reference)

Expand Down
16 changes: 8 additions & 8 deletions pandas/tests/io/parser/test_unsupported.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def test_on_bad_lines_callable_python_or_pyarrow(self, all_parsers):
parser.read_csv(sio, on_bad_lines=bad_lines_func)


def test_close_file_handle_on_invalid_usecols(all_parsers):
def test_close_file_handle_on_invalid_usecols(all_parsers, temp_file):
# GH 45384
parser = all_parsers

Expand All @@ -176,13 +176,13 @@ def test_close_file_handle_on_invalid_usecols(all_parsers):
# Raises pyarrow.lib.ArrowKeyError
pytest.skip(reason="https://github.com/apache/arrow/issues/38676")

with tm.ensure_clean("test.csv") as fname:
Path(fname).write_text("col1,col2\na,b\n1,2", encoding="utf-8")
with tm.assert_produces_warning(False):
with pytest.raises(error, match="col3"):
parser.read_csv(fname, usecols=["col1", "col2", "col3"])
# unlink fails on windows if file handles still point to it
os.unlink(fname)
fname = temp_file
Path(fname).write_text("col1,col2\na,b\n1,2", encoding="utf-8")
with tm.assert_produces_warning(False):
with pytest.raises(error, match="col3"):
parser.read_csv(fname, usecols=["col1", "col2", "col3"])
# unlink fails on windows if file handles still point to it
os.unlink(fname)


def test_invalid_file_inputs(request, all_parsers):
Expand Down
Loading