Skip to content

Commit 52ee1a8

Browse files
fix str type cast in temp file in frame methods to csv
1 parent 43bff1b commit 52ee1a8

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

pandas/tests/frame/methods/test_to_csv.py

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -261,15 +261,13 @@ def _return_result_expected(
261261
kwargs["index_col"] = list(range(rnlvl))
262262
kwargs["header"] = list(range(cnlvl))
263263

264-
path = str(temp_file)
265-
df.to_csv(path, encoding="utf8", chunksize=chunksize)
266-
recons = self.read_csv(path, **kwargs)
264+
df.to_csv(temp_file, encoding="utf8", chunksize=chunksize)
265+
recons = self.read_csv(temp_file, **kwargs)
267266
else:
268267
kwargs["header"] = 0
269268

270-
path = str(temp_file)
271-
df.to_csv(path, encoding="utf8", chunksize=chunksize)
272-
recons = self.read_csv(path, **kwargs)
269+
df.to_csv(temp_file, encoding="utf8", chunksize=chunksize)
270+
recons = self.read_csv(temp_file, **kwargs)
273271

274272
def _to_uni(x):
275273
if not isinstance(x, str):
@@ -624,8 +622,8 @@ def _make_frame(names=None):
624622
[[f"i-{i}" for i in range(5)] for _ in range(2)], names=list("ab")
625623
),
626624
)
627-
df.to_csv(path)
628-
result = read_csv(path, header=[0, 1, 2, 3], index_col=[0, 1])
625+
df.to_csv(temp_file)
626+
result = read_csv(temp_file, header=[0, 1, 2, 3], index_col=[0, 1])
629627
tm.assert_frame_equal(df, result)
630628

631629
# column is mi
@@ -635,8 +633,8 @@ def _make_frame(names=None):
635633
[[f"i-{i}" for i in range(3)] for _ in range(4)], names=list("abcd")
636634
),
637635
)
638-
df.to_csv(path)
639-
result = read_csv(path, header=[0, 1, 2, 3], index_col=0)
636+
df.to_csv(temp_file)
637+
result = read_csv(temp_file, header=[0, 1, 2, 3], index_col=0)
640638
tm.assert_frame_equal(df, result)
641639

642640
# dup column names?
@@ -649,52 +647,52 @@ def _make_frame(names=None):
649647
[[f"i-{i}" for i in range(5)] for _ in range(3)], names=list("abc")
650648
),
651649
)
652-
df.to_csv(path)
653-
result = read_csv(path, header=[0, 1, 2, 3], index_col=[0, 1, 2])
650+
df.to_csv(temp_file)
651+
result = read_csv(temp_file, header=[0, 1, 2, 3], index_col=[0, 1, 2])
654652
tm.assert_frame_equal(df, result)
655653

656654
# writing with no index
657655
df = _make_frame()
658-
df.to_csv(path, index=False)
659-
result = read_csv(path, header=[0, 1])
656+
df.to_csv(temp_file, index=False)
657+
result = read_csv(temp_file, header=[0, 1])
660658
tm.assert_frame_equal(df, result)
661659

662660
# we lose the names here
663661
df = _make_frame(True)
664-
df.to_csv(path, index=False)
665-
result = read_csv(path, header=[0, 1])
662+
df.to_csv(temp_file, index=False)
663+
result = read_csv(temp_file, header=[0, 1])
666664
assert com.all_none(*result.columns.names)
667665
result.columns.names = df.columns.names
668666
tm.assert_frame_equal(df, result)
669667

670668
# whatsnew example
671669
df = _make_frame()
672-
df.to_csv(path)
673-
result = read_csv(path, header=[0, 1], index_col=[0])
670+
df.to_csv(temp_file)
671+
result = read_csv(temp_file, header=[0, 1], index_col=[0])
674672
tm.assert_frame_equal(df, result)
675673

676674
df = _make_frame(True)
677-
df.to_csv(path)
678-
result = read_csv(path, header=[0, 1], index_col=[0])
675+
df.to_csv(temp_file)
676+
result = read_csv(temp_file, header=[0, 1], index_col=[0])
679677
tm.assert_frame_equal(df, result)
680678

681679
# invalid options
682680
df = _make_frame(True)
683-
df.to_csv(path)
681+
df.to_csv(temp_file)
684682

685683
for i in [6, 7]:
686684
msg = f"len of {i}, but only 5 lines in file"
687685
with pytest.raises(ParserError, match=msg):
688-
read_csv(path, header=list(range(i)), index_col=0)
686+
read_csv(temp_file, header=list(range(i)), index_col=0)
689687

690688
# write with cols
691689
msg = "cannot specify cols with a MultiIndex"
692690
with pytest.raises(TypeError, match=msg):
693-
df.to_csv(path, columns=["foo", "bar"])
691+
df.to_csv(temp_file, columns=["foo", "bar"])
694692

695693
# empty
696-
tsframe[:0].to_csv(path)
697-
recons = self.read_csv(path)
694+
tsframe[:0].to_csv(temp_file)
695+
recons = self.read_csv(temp_file)
698696

699697
exp = tsframe[:0]
700698
exp.index = []
@@ -812,8 +810,8 @@ def test_to_csv_dups_cols(self, temp_file):
812810

813811
df.columns = [0, 1, 2] * 5
814812

815-
df.to_csv(path)
816-
result = read_csv(path, index_col=0)
813+
df.to_csv(temp_file)
814+
result = read_csv(temp_file, index_col=0)
817815

818816
# date cols
819817
for i in ["0.4", "1.4", "2.4"]:
@@ -1201,9 +1199,8 @@ def test_to_csv_with_dst_transitions_with_pickle(self, start, end, temp_file):
12011199
idx._data._freq = None # otherwise there is trouble on unpickle
12021200
df = DataFrame({"values": 1, "idx": idx}, index=idx)
12031201

1204-
path = str(temp_file)
1205-
df.to_csv(path, index=True)
1206-
result = read_csv(path, index_col=0)
1202+
df.to_csv(temp_file, index=True)
1203+
result = read_csv(temp_file, index_col=0)
12071204
result.index = (
12081205
to_datetime(result.index, utc=True).tz_convert("Europe/Paris").as_unit("ns")
12091206
)

0 commit comments

Comments
 (0)