Skip to content

Commit c9ee1c1

Browse files
fix str type cast for temp file in io format to csv
1 parent 52ee1a8 commit c9ee1c1

File tree

1 file changed

+45
-58
lines changed

1 file changed

+45
-58
lines changed

pandas/tests/io/formats/test_to_csv.py

Lines changed: 45 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,26 @@ def test_to_csv_with_single_column(self, temp_file):
3030
""
3131
1.0
3232
"""
33-
path = str(temp_file)
34-
df1.to_csv(path, header=None, index=None)
35-
with open(path, encoding="utf-8") as f:
33+
df1.to_csv(temp_file, header=None, index=None)
34+
with open(temp_file, encoding="utf-8") as f:
3635
assert f.read() == expected1
3736

3837
df2 = DataFrame([1, None])
3938
expected2 = """\
4039
1.0
4140
""
4241
"""
43-
df2.to_csv(path, header=None, index=None)
44-
with open(path, encoding="utf-8") as f:
42+
df2.to_csv(temp_file, header=None, index=None)
43+
with open(temp_file, encoding="utf-8") as f:
4544
assert f.read() == expected2
4645

4746
def test_to_csv_default_encoding(self, temp_file):
4847
# GH17097
4948
df = DataFrame({"col": ["AAAAA", "ÄÄÄÄÄ", "ßßßßß", "聞聞聞聞聞"]})
5049

51-
path = str(temp_file)
5250
# the default to_csv encoding is uft-8.
53-
df.to_csv(path)
54-
tm.assert_frame_equal(pd.read_csv(path, index_col=0), df)
51+
df.to_csv(temp_file)
52+
tm.assert_frame_equal(pd.read_csv(temp_file, index_col=0), df)
5553

5654
def test_to_csv_quotechar(self, temp_file):
5755
df = DataFrame({"col": [1, 2]})
@@ -61,9 +59,8 @@ def test_to_csv_quotechar(self, temp_file):
6159
"1","2"
6260
"""
6361

64-
path = str(temp_file)
65-
df.to_csv(path, quoting=1) # 1=QUOTE_ALL
66-
with open(path, encoding="utf-8") as f:
62+
df.to_csv(temp_file, quoting=1) # 1=QUOTE_ALL
63+
with open(temp_file, encoding="utf-8") as f:
6764
assert f.read() == expected
6865

6966
expected = """\
@@ -72,13 +69,12 @@ def test_to_csv_quotechar(self, temp_file):
7269
$1$,$2$
7370
"""
7471

75-
path = str(temp_file)
76-
df.to_csv(path, quoting=1, quotechar="$")
77-
with open(path, encoding="utf-8") as f:
72+
df.to_csv(temp_file, quoting=1, quotechar="$")
73+
with open(temp_file, encoding="utf-8") as f:
7874
assert f.read() == expected
7975

8076
with pytest.raises(TypeError, match="quotechar"):
81-
df.to_csv(path, quoting=1, quotechar=None)
77+
df.to_csv(temp_file, quoting=1, quotechar=None)
8278

8379
def test_to_csv_doublequote(self, temp_file):
8480
df = DataFrame({"col": ['a"a', '"bb"']})
@@ -88,13 +84,12 @@ def test_to_csv_doublequote(self, temp_file):
8884
"1","""bb"""
8985
'''
9086

91-
path = str(temp_file)
92-
df.to_csv(path, quoting=1, doublequote=True) # QUOTE_ALL
93-
with open(path, encoding="utf-8") as f:
87+
df.to_csv(temp_file, quoting=1, doublequote=True) # QUOTE_ALL
88+
with open(temp_file, encoding="utf-8") as f:
9489
assert f.read() == expected
9590

9691
with pytest.raises(Error, match="escapechar"):
97-
df.to_csv(path, doublequote=False) # no escapechar set
92+
df.to_csv(temp_file, doublequote=False) # no escapechar set
9893

9994
def test_to_csv_escapechar(self, temp_file):
10095
df = DataFrame({"col": ['a"a', '"bb"']})
@@ -104,9 +99,8 @@ def test_to_csv_escapechar(self, temp_file):
10499
"1","\\"bb\\""
105100
"""
106101

107-
path = str(temp_file)
108-
df.to_csv(path, quoting=1, doublequote=False, escapechar="\\")
109-
with open(path, encoding="utf-8") as f:
102+
df.to_csv(temp_file, quoting=1, doublequote=False, escapechar="\\")
103+
with open(temp_file, encoding="utf-8") as f:
110104
assert f.read() == expected
111105

112106
df = DataFrame({"col": ["a,a", ",bb,"]})
@@ -116,8 +110,8 @@ def test_to_csv_escapechar(self, temp_file):
116110
1,\\,bb\\,
117111
"""
118112

119-
df.to_csv(path, quoting=3, escapechar="\\") # QUOTE_NONE
120-
with open(path, encoding="utf-8") as f:
113+
df.to_csv(temp_file, quoting=3, escapechar="\\") # QUOTE_NONE
114+
with open(temp_file, encoding="utf-8") as f:
121115
assert f.read() == expected
122116

123117
def test_csv_to_string(self):
@@ -395,9 +389,8 @@ def test_to_csv_string_array_ascii(self, temp_file):
395389
0,"['foo', 'bar']"
396390
1,"['baz', 'qux']"
397391
"""
398-
path = str(temp_file)
399-
df.to_csv(path, encoding="ascii")
400-
with open(path, encoding="utf-8") as f:
392+
df.to_csv(temp_file, encoding="ascii")
393+
with open(temp_file, encoding="utf-8") as f:
401394
assert f.read() == expected_ascii
402395

403396
def test_to_csv_string_array_utf8(self, temp_file):
@@ -409,16 +402,14 @@ def test_to_csv_string_array_utf8(self, temp_file):
409402
0,"['foo', 'bar']"
410403
1,"['baz', 'qux']"
411404
"""
412-
path = str(temp_file)
413-
df.to_csv(path, encoding="utf-8")
414-
with open(path, encoding="utf-8") as f:
405+
df.to_csv(temp_file, encoding="utf-8")
406+
with open(temp_file, encoding="utf-8") as f:
415407
assert f.read() == expected_utf8
416408

417409
def test_to_csv_string_with_lf(self, temp_file):
418410
# GH 20353
419411
data = {"int": [1, 2, 3], "str_lf": ["abc", "d\nef", "g\nh\n\ni"]}
420412
df = DataFrame(data)
421-
path = str(temp_file)
422413

423414
# case 1: The default line terminator(=os.linesep)(PR 21406)
424415
os_linesep = os.linesep.encode("utf-8")
@@ -432,28 +423,27 @@ def test_to_csv_string_with_lf(self, temp_file):
432423
+ b'3,"g\nh\n\ni"'
433424
+ os_linesep
434425
)
435-
df.to_csv(path, index=False)
436-
with open(path, "rb") as f:
426+
df.to_csv(temp_file, index=False)
427+
with open(temp_file, "rb") as f:
437428
assert f.read() == expected_noarg
438429

439430
# case 2: LF as line terminator
440431
expected_lf = b'int,str_lf\n1,abc\n2,"d\nef"\n3,"g\nh\n\ni"\n'
441-
df.to_csv(path, lineterminator="\n", index=False)
442-
with open(path, "rb") as f:
432+
df.to_csv(temp_file, lineterminator="\n", index=False)
433+
with open(temp_file, "rb") as f:
443434
assert f.read() == expected_lf
444435

445436
# case 3: CRLF as line terminator
446437
# 'lineterminator' should not change inner element
447438
expected_crlf = b'int,str_lf\r\n1,abc\r\n2,"d\nef"\r\n3,"g\nh\n\ni"\r\n'
448-
df.to_csv(path, lineterminator="\r\n", index=False)
449-
with open(path, "rb") as f:
439+
df.to_csv(temp_file, lineterminator="\r\n", index=False)
440+
with open(temp_file, "rb") as f:
450441
assert f.read() == expected_crlf
451442

452443
def test_to_csv_string_with_crlf(self, temp_file):
453444
# GH 20353
454445
data = {"int": [1, 2, 3], "str_crlf": ["abc", "d\r\nef", "g\r\nh\r\n\r\ni"]}
455446
df = DataFrame(data)
456-
path = str(temp_file)
457447
# case 1: The default line terminator(=os.linesep)(PR 21406)
458448
os_linesep = os.linesep.encode("utf-8")
459449
expected_noarg = (
@@ -466,23 +456,23 @@ def test_to_csv_string_with_crlf(self, temp_file):
466456
+ b'3,"g\r\nh\r\n\r\ni"'
467457
+ os_linesep
468458
)
469-
df.to_csv(path, index=False)
470-
with open(path, "rb") as f:
459+
df.to_csv(temp_file, index=False)
460+
with open(temp_file, "rb") as f:
471461
assert f.read() == expected_noarg
472462

473463
# case 2: LF as line terminator
474464
expected_lf = b'int,str_crlf\n1,abc\n2,"d\r\nef"\n3,"g\r\nh\r\n\r\ni"\n'
475-
df.to_csv(path, lineterminator="\n", index=False)
476-
with open(path, "rb") as f:
465+
df.to_csv(temp_file, lineterminator="\n", index=False)
466+
with open(temp_file, "rb") as f:
477467
assert f.read() == expected_lf
478468

479469
# case 3: CRLF as line terminator
480470
# 'lineterminator' should not change inner element
481471
expected_crlf = (
482472
b'int,str_crlf\r\n1,abc\r\n2,"d\r\nef"\r\n3,"g\r\nh\r\n\r\ni"\r\n'
483473
)
484-
df.to_csv(path, lineterminator="\r\n", index=False)
485-
with open(path, "rb") as f:
474+
df.to_csv(temp_file, lineterminator="\r\n", index=False)
475+
with open(temp_file, "rb") as f:
486476
assert f.read() == expected_crlf
487477

488478
def test_to_csv_stdout_file(self, capsys):
@@ -514,11 +504,10 @@ def test_to_csv_write_to_open_file(self, temp_file):
514504
y
515505
z
516506
"""
517-
path = str(temp_file)
518-
with open(path, "w", encoding="utf-8") as f:
507+
with open(temp_file, "w", encoding="utf-8") as f:
519508
f.write("manual header\n")
520509
df.to_csv(f, header=None, index=None)
521-
with open(path, encoding="utf-8") as f:
510+
with open(temp_file, encoding="utf-8") as f:
522511
assert f.read() == expected
523512

524513
def test_to_csv_write_to_open_file_with_newline_py3(self, temp_file):
@@ -528,12 +517,11 @@ def test_to_csv_write_to_open_file_with_newline_py3(self, temp_file):
528517
expected_rows = ["x", "y", "z"]
529518
expected = "manual header\n" + tm.convert_rows_list_to_csv_str(expected_rows)
530519

531-
path = str(temp_file)
532-
with open(path, "w", newline="", encoding="utf-8") as f:
520+
with open(temp_file, "w", newline="", encoding="utf-8") as f:
533521
f.write("manual header\n")
534522
df.to_csv(f, header=None, index=None)
535523

536-
with open(path, "rb") as f:
524+
with open(temp_file, "rb") as f:
537525
assert f.read() == bytes(expected, "utf-8")
538526

539527
@pytest.mark.parametrize("to_infer", [True, False])
@@ -664,8 +652,7 @@ def test_to_csv_errors(self, errors, temp_file):
664652
data = ["\ud800foo"]
665653
ser = pd.Series(data, index=Index(data, dtype=object), dtype=object)
666654

667-
path = str(temp_file)
668-
ser.to_csv(path, errors=errors)
655+
ser.to_csv(temp_file, errors=errors)
669656
# No use in reading back the data as it is not the same anymore
670657
# due to the error handling
671658

@@ -683,10 +670,9 @@ def test_to_csv_binary_handle(self, mode, temp_file):
683670
index=Index([f"i-{i}" for i in range(30)]),
684671
)
685672

686-
path = str(temp_file)
687-
with open(path, mode="w+b") as handle:
673+
with open(temp_file, mode="w+b") as handle:
688674
df.to_csv(handle, mode=mode)
689-
tm.assert_frame_equal(df, pd.read_csv(path, index_col=0))
675+
tm.assert_frame_equal(df, pd.read_csv(temp_file, index_col=0))
690676

691677
@pytest.mark.parametrize("mode", ["wb", "w"])
692678
def test_to_csv_encoding_binary_handle(self, mode, temp_file):
@@ -721,9 +707,10 @@ def test_to_csv_iterative_compression_name(compression, temp_file):
721707
columns=Index(list("ABCD")),
722708
index=Index([f"i-{i}" for i in range(30)]),
723709
)
724-
path = str(temp_file)
725-
df.to_csv(path, compression=compression, chunksize=1)
726-
tm.assert_frame_equal(pd.read_csv(path, compression=compression, index_col=0), df)
710+
df.to_csv(temp_file, compression=compression, chunksize=1)
711+
tm.assert_frame_equal(
712+
pd.read_csv(temp_file, compression=compression, index_col=0), df
713+
)
727714

728715

729716
def test_to_csv_iterative_compression_buffer(compression):

0 commit comments

Comments
 (0)