Skip to content

Commit 2d12692

Browse files
committed
PR fixes
1 parent fe57a8c commit 2d12692

File tree

5 files changed

+44
-64
lines changed

5 files changed

+44
-64
lines changed

pandas/tests/io/formats/style/test_style.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,5 +1599,4 @@ def test_no_empty_apply(mi_styler):
15991599
@pytest.mark.parametrize("format", ["html", "latex", "string"])
16001600
def test_output_buffer(mi_styler, format, temp_file):
16011601
# gh 47053
1602-
f = str(temp_file)
1603-
getattr(mi_styler, f"to_{format}")(f)
1602+
getattr(mi_styler, f"to_{format}")(temp_file)

pandas/tests/io/formats/test_to_latex.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,22 @@ def df_short():
3030

3131
class TestToLatex:
3232
def test_to_latex_to_file(self, float_frame, temp_file):
33-
path = str(temp_file)
34-
float_frame.to_latex(path)
35-
with open(path, encoding="utf-8") as f:
33+
float_frame.to_latex(temp_file)
34+
with open(temp_file, encoding="utf-8") as f:
3635
assert float_frame.to_latex() == f.read()
3736

3837
def test_to_latex_to_file_utf8_with_encoding(self, temp_file):
3938
# test with utf-8 and encoding option (GH 7061)
4039
df = DataFrame([["au\xdfgangen"]])
41-
path = str(temp_file)
42-
df.to_latex(path, encoding="utf-8")
43-
with open(path, encoding="utf-8") as f:
40+
df.to_latex(temp_file, encoding="utf-8")
41+
with open(temp_file, encoding="utf-8") as f:
4442
assert df.to_latex() == f.read()
4543

4644
def test_to_latex_to_file_utf8_without_encoding(self, temp_file):
4745
# test with utf-8 without encoding option
4846
df = DataFrame([["au\xdfgangen"]])
49-
path = str(temp_file)
50-
df.to_latex(path)
51-
with open(path, encoding="utf-8") as f:
47+
df.to_latex(temp_file)
48+
with open(temp_file, encoding="utf-8") as f:
5249
assert df.to_latex() == f.read()
5350

5451
def test_to_latex_tabular_with_index(self):

pandas/tests/io/json/test_readlines.py

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -204,22 +204,20 @@ def test_readjson_chunks_from_file(request, engine, temp_file):
204204
)
205205
request.applymarker(pytest.mark.xfail(reason=reason, raises=ValueError))
206206

207-
path = str(temp_file)
208207
df = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
209-
df.to_json(path, lines=True, orient="records")
210-
with read_json(path, lines=True, chunksize=1, engine=engine) as reader:
208+
df.to_json(temp_file, lines=True, orient="records")
209+
with read_json(temp_file, lines=True, chunksize=1, engine=engine) as reader:
211210
chunked = pd.concat(reader)
212-
unchunked = read_json(path, lines=True, engine=engine)
211+
unchunked = read_json(temp_file, lines=True, engine=engine)
213212
tm.assert_frame_equal(unchunked, chunked)
214213

215214

216215
@pytest.mark.parametrize("chunksize", [None, 1])
217216
def test_readjson_chunks_closes(chunksize, temp_file):
218-
path = str(temp_file)
219217
df = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
220-
df.to_json(path, lines=True, orient="records")
218+
df.to_json(temp_file, lines=True, orient="records")
221219
reader = JsonReader(
222-
path,
220+
temp_file,
223221
orient=None,
224222
typ="frame",
225223
dtype=True,
@@ -287,12 +285,11 @@ def test_readjson_unicode(request, monkeypatch, engine, temp_file):
287285
)
288286
request.applymarker(pytest.mark.xfail(reason=reason, raises=ValueError))
289287

290-
path = str(temp_file)
291288
monkeypatch.setattr("locale.getpreferredencoding", lambda do_setlocale: "cp949")
292-
with open(path, "w", encoding="utf-8") as f:
289+
with open(temp_file, "w", encoding="utf-8") as f:
293290
f.write('{"£©µÀÆÖÞßéöÿ":["АБВГДабвгд가"]}')
294291

295-
result = read_json(path, engine=engine)
292+
result = read_json(temp_file, engine=engine)
296293
expected = DataFrame({"£©µÀÆÖÞßéöÿ": ["АБВГДабвгд가"]})
297294
tm.assert_frame_equal(result, expected)
298295

@@ -449,13 +446,12 @@ def test_to_json_append_output_consistent_columns(temp_file):
449446
df2 = DataFrame({"col1": [3, 4], "col2": ["c", "d"]})
450447

451448
expected = DataFrame({"col1": [1, 2, 3, 4], "col2": ["a", "b", "c", "d"]})
452-
path = str(temp_file)
453449
# Save dataframes to the same file
454-
df1.to_json(path, lines=True, orient="records")
455-
df2.to_json(path, mode="a", lines=True, orient="records")
450+
df1.to_json(temp_file, lines=True, orient="records")
451+
df2.to_json(temp_file, mode="a", lines=True, orient="records")
456452

457453
# Read path file
458-
result = read_json(path, lines=True)
454+
result = read_json(temp_file, lines=True)
459455
tm.assert_frame_equal(result, expected)
460456

461457

@@ -473,13 +469,12 @@ def test_to_json_append_output_inconsistent_columns(temp_file):
473469
"col3": [np.nan, np.nan, "!", "#"],
474470
}
475471
)
476-
path = str(temp_file)
477472
# Save dataframes to the same file
478-
df1.to_json(path, mode="a", lines=True, orient="records")
479-
df3.to_json(path, mode="a", lines=True, orient="records")
473+
df1.to_json(temp_file, mode="a", lines=True, orient="records")
474+
df3.to_json(temp_file, mode="a", lines=True, orient="records")
480475

481476
# Read path file
482-
result = read_json(path, lines=True)
477+
result = read_json(temp_file, lines=True)
483478
tm.assert_frame_equal(result, expected)
484479

485480

@@ -500,15 +495,14 @@ def test_to_json_append_output_different_columns(temp_file):
500495
"col4": [None, None, None, None, None, None, True, False],
501496
}
502497
).astype({"col4": "float"})
503-
path = str(temp_file)
504498
# Save dataframes to the same file
505-
df1.to_json(path, mode="a", lines=True, orient="records")
506-
df2.to_json(path, mode="a", lines=True, orient="records")
507-
df3.to_json(path, mode="a", lines=True, orient="records")
508-
df4.to_json(path, mode="a", lines=True, orient="records")
499+
df1.to_json(temp_file, mode="a", lines=True, orient="records")
500+
df2.to_json(temp_file, mode="a", lines=True, orient="records")
501+
df3.to_json(temp_file, mode="a", lines=True, orient="records")
502+
df4.to_json(temp_file, mode="a", lines=True, orient="records")
509503

510504
# Read path file
511-
result = read_json(path, lines=True)
505+
result = read_json(temp_file, lines=True)
512506
tm.assert_frame_equal(result, expected)
513507

514508

@@ -530,13 +524,12 @@ def test_to_json_append_output_different_columns_reordered(temp_file):
530524
"col1": [None, None, None, None, 3, 4, 1, 2],
531525
}
532526
).astype({"col4": "float"})
533-
path = str(temp_file)
534527
# Save dataframes to the same file
535-
df4.to_json(path, mode="a", lines=True, orient="records")
536-
df3.to_json(path, mode="a", lines=True, orient="records")
537-
df2.to_json(path, mode="a", lines=True, orient="records")
538-
df1.to_json(path, mode="a", lines=True, orient="records")
528+
df4.to_json(temp_file, mode="a", lines=True, orient="records")
529+
df3.to_json(temp_file, mode="a", lines=True, orient="records")
530+
df2.to_json(temp_file, mode="a", lines=True, orient="records")
531+
df1.to_json(temp_file, mode="a", lines=True, orient="records")
539532

540533
# Read path file
541-
result = read_json(path, lines=True)
534+
result = read_json(temp_file, lines=True)
542535
tm.assert_frame_equal(result, expected)

pandas/tests/io/test_feather.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,14 @@ def check_error_on_write(self, df, exc, err_msg, temp_file):
3131
# on writing
3232

3333
with pytest.raises(exc, match=err_msg):
34-
path = str(temp_file)
35-
to_feather(df, path)
34+
to_feather(df, temp_file)
3635

3736
def check_external_error_on_write(self, df, temp_file):
3837
# check that we are raising the exception
3938
# on writing
4039

4140
with tm.external_error_raised(Exception):
42-
path = str(temp_file)
43-
to_feather(df, path)
41+
to_feather(df, temp_file)
4442

4543
def check_round_trip(
4644
self, df, temp_file, expected=None, write_kwargs=None, **read_kwargs
@@ -50,10 +48,9 @@ def check_round_trip(
5048
if expected is None:
5149
expected = df.copy()
5250

53-
path = str(temp_file)
54-
to_feather(df, path, **write_kwargs)
51+
to_feather(df, temp_file, **write_kwargs)
5552

56-
result = read_feather(path, **read_kwargs)
53+
result = read_feather(temp_file, **read_kwargs)
5754

5855
tm.assert_frame_equal(result, expected)
5956

@@ -186,10 +183,9 @@ def test_read_feather_dtype_backend(
186183
}
187184
)
188185

189-
path = str(temp_file)
190-
to_feather(df, path)
186+
to_feather(df, temp_file)
191187
with pd.option_context("mode.string_storage", string_storage):
192-
result = read_feather(path, dtype_backend=dtype_backend)
188+
result = read_feather(temp_file, dtype_backend=dtype_backend)
193189

194190
if dtype_backend == "pyarrow":
195191
pa = pytest.importorskip("pyarrow")
@@ -239,10 +235,9 @@ def test_invalid_dtype_backend(self, temp_file):
239235
"'pyarrow' are allowed."
240236
)
241237
df = pd.DataFrame({"int": list(range(1, 4))})
242-
path = str(temp_file)
243-
df.to_feather(path)
238+
df.to_feather(temp_file)
244239
with pytest.raises(ValueError, match=msg):
245-
read_feather(path, dtype_backend="numpy")
240+
read_feather(temp_file, dtype_backend="numpy")
246241

247242
def test_string_inference(self, tmp_path, using_infer_string):
248243
# GH#54431

pandas/tests/io/test_orc.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from decimal import Decimal
55
from io import BytesIO
66
import os
7-
import pathlib
87

98
import numpy as np
109
import pytest
@@ -249,9 +248,8 @@ def test_orc_roundtrip_file(dirpath, temp_file):
249248
}
250249
expected = pd.DataFrame.from_dict(data)
251250

252-
path = str(temp_file)
253-
expected.to_orc(path)
254-
got = read_orc(path)
251+
expected.to_orc(temp_file)
252+
got = read_orc(temp_file)
255253

256254
tm.assert_equal(expected, got)
257255

@@ -385,9 +383,8 @@ def test_orc_dtype_backend_numpy_nullable():
385383

386384
def test_orc_uri_path(temp_file):
387385
expected = pd.DataFrame({"int": list(range(1, 4))})
388-
path = str(temp_file)
389-
expected.to_orc(path)
390-
uri = pathlib.Path(path).as_uri()
386+
expected.to_orc(temp_file)
387+
uri = temp_file.as_uri()
391388
result = read_orc(uri)
392389
tm.assert_frame_equal(result, expected)
393390

@@ -416,10 +413,9 @@ def test_invalid_dtype_backend(temp_file):
416413
"'pyarrow' are allowed."
417414
)
418415
df = pd.DataFrame({"int": list(range(1, 4))})
419-
path = str(temp_file)
420-
df.to_orc(path)
416+
df.to_orc(temp_file)
421417
with pytest.raises(ValueError, match=msg):
422-
read_orc(path, dtype_backend="numpy")
418+
read_orc(temp_file, dtype_backend="numpy")
423419

424420

425421
def test_string_inference(tmp_path):

0 commit comments

Comments
 (0)