@@ -248,6 +248,7 @@ def _return_result_expected(
248
248
self ,
249
249
df ,
250
250
chunksize ,
251
+ temp_file ,
251
252
r_dtype = None ,
252
253
c_dtype = None ,
253
254
rnlvl = None ,
@@ -260,15 +261,13 @@ def _return_result_expected(
260
261
kwargs ["index_col" ] = list (range (rnlvl ))
261
262
kwargs ["header" ] = list (range (cnlvl ))
262
263
263
- with tm .ensure_clean ("__tmp_to_csv_moar__" ) as path :
264
- df .to_csv (path , encoding = "utf8" , chunksize = chunksize )
265
- recons = self .read_csv (path , ** kwargs )
264
+ df .to_csv (temp_file , encoding = "utf8" , chunksize = chunksize )
265
+ recons = self .read_csv (temp_file , ** kwargs )
266
266
else :
267
267
kwargs ["header" ] = 0
268
268
269
- with tm .ensure_clean ("__tmp_to_csv_moar__" ) as path :
270
- df .to_csv (path , encoding = "utf8" , chunksize = chunksize )
271
- recons = self .read_csv (path , ** kwargs )
269
+ df .to_csv (temp_file , encoding = "utf8" , chunksize = chunksize )
270
+ recons = self .read_csv (temp_file , ** kwargs )
272
271
273
272
def _to_uni (x ):
274
273
if not isinstance (x , str ):
@@ -353,13 +352,13 @@ def _to_uni(x):
353
352
@pytest .mark .parametrize (
354
353
"nrows" , [2 , 10 , 99 , 100 , 101 , 102 , 198 , 199 , 200 , 201 , 202 , 249 , 250 , 251 ]
355
354
)
356
- def test_to_csv_nrows (self , nrows ):
355
+ def test_to_csv_nrows (self , nrows , temp_file ):
357
356
df = DataFrame (
358
357
np .ones ((nrows , 4 )),
359
358
index = date_range ("2020年01月01日" , periods = nrows ),
360
359
columns = Index (list ("abcd" ), dtype = object ),
361
360
)
362
- result , expected = self ._return_result_expected (df , 1000 , "dt" , "s" )
361
+ result , expected = self ._return_result_expected (df , 1000 , temp_file , "dt" , "s" )
363
362
expected .index = expected .index .astype ("M8[ns]" )
364
363
tm .assert_frame_equal (result , expected , check_names = False )
365
364
@@ -372,7 +371,7 @@ def test_to_csv_nrows(self, nrows):
372
371
)
373
372
@pytest .mark .parametrize ("ncols" , [1 , 2 , 3 , 4 ])
374
373
@pytest .mark .filterwarnings (r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning" )
375
- def test_to_csv_idx_types (self , nrows , r_idx_type , c_idx_type , ncols ):
374
+ def test_to_csv_idx_types (self , nrows , r_idx_type , c_idx_type , ncols , temp_file ):
376
375
axes = {
377
376
"i" : lambda n : Index (np .arange (n ), dtype = np .int64 ),
378
377
"s" : lambda n : Index ([f"{ i } _{ chr (i )} " for i in range (97 , 97 + n )]),
@@ -387,6 +386,7 @@ def test_to_csv_idx_types(self, nrows, r_idx_type, c_idx_type, ncols):
387
386
result , expected = self ._return_result_expected (
388
387
df ,
389
388
1000 ,
389
+ temp_file ,
390
390
r_idx_type ,
391
391
c_idx_type ,
392
392
)
@@ -401,18 +401,18 @@ def test_to_csv_idx_types(self, nrows, r_idx_type, c_idx_type, ncols):
401
401
"nrows" , [10 , 98 , 99 , 100 , 101 , 102 , 198 , 199 , 200 , 201 , 202 , 249 , 250 , 251 ]
402
402
)
403
403
@pytest .mark .parametrize ("ncols" , [1 , 2 , 3 , 4 ])
404
- def test_to_csv_idx_ncols (self , nrows , ncols ):
404
+ def test_to_csv_idx_ncols (self , nrows , ncols , temp_file ):
405
405
df = DataFrame (
406
406
np .ones ((nrows , ncols )),
407
407
index = Index ([f"i-{ i } " for i in range (nrows )], name = "a" ),
408
408
columns = Index ([f"i-{ i } " for i in range (ncols )], name = "a" ),
409
409
)
410
- result , expected = self ._return_result_expected (df , 1000 )
410
+ result , expected = self ._return_result_expected (df , 1000 , temp_file )
411
411
tm .assert_frame_equal (result , expected , check_names = False )
412
412
413
413
@pytest .mark .slow
414
414
@pytest .mark .parametrize ("nrows" , [10 , 98 , 99 , 100 , 101 , 102 ])
415
- def test_to_csv_dup_cols (self , nrows ):
415
+ def test_to_csv_dup_cols (self , nrows , temp_file ):
416
416
df = DataFrame (
417
417
np .ones ((nrows , 3 )),
418
418
index = Index ([f"i-{ i } " for i in range (nrows )], name = "a" ),
@@ -427,25 +427,29 @@ def test_to_csv_dup_cols(self, nrows):
427
427
ix [- 2 :] = ["rdupe" , "rdupe" ]
428
428
df .index = ix
429
429
df .columns = cols
430
- result , expected = self ._return_result_expected (df , 1000 , dupe_col = True )
430
+ result , expected = self ._return_result_expected (
431
+ df , 1000 , temp_file , dupe_col = True
432
+ )
431
433
tm .assert_frame_equal (result , expected , check_names = False )
432
434
433
435
@pytest .mark .slow
434
- def test_to_csv_empty (self ):
436
+ def test_to_csv_empty (self , temp_file ):
435
437
df = DataFrame (index = np .arange (10 , dtype = np .int64 ))
436
- result , expected = self ._return_result_expected (df , 1000 )
438
+ result , expected = self ._return_result_expected (df , 1000 , temp_file )
437
439
tm .assert_frame_equal (result , expected , check_column_type = False )
438
440
439
441
@pytest .mark .slow
440
- def test_to_csv_chunksize (self ):
442
+ def test_to_csv_chunksize (self , temp_file ):
441
443
chunksize = 1000
442
444
rows = chunksize // 2 + 1
443
445
df = DataFrame (
444
446
np .ones ((rows , 2 )),
445
447
columns = Index (list ("ab" )),
446
448
index = MultiIndex .from_arrays ([range (rows ) for _ in range (2 )]),
447
449
)
448
- result , expected = self ._return_result_expected (df , chunksize , rnlvl = 2 )
450
+ result , expected = self ._return_result_expected (
451
+ df , chunksize , temp_file , rnlvl = 2
452
+ )
449
453
tm .assert_frame_equal (result , expected , check_names = False )
450
454
451
455
@pytest .mark .slow
@@ -461,7 +465,7 @@ def test_to_csv_chunksize(self):
461
465
[{"r_idx_nlevels" : 2 , "c_idx_nlevels" : 2 }, {"rnlvl" : 2 , "cnlvl" : 2 }],
462
466
],
463
467
)
464
- def test_to_csv_params (self , nrows , df_params , func_params , ncols ):
468
+ def test_to_csv_params (self , nrows , df_params , func_params , ncols , temp_file ):
465
469
if df_params .get ("r_idx_nlevels" ):
466
470
index = MultiIndex .from_arrays (
467
471
[f"i-{ i } " for i in range (nrows )]
@@ -478,7 +482,9 @@ def test_to_csv_params(self, nrows, df_params, func_params, ncols):
478
482
else :
479
483
columns = Index ([f"i-{ i } " for i in range (ncols )])
480
484
df = DataFrame (np .ones ((nrows , ncols )), index = index , columns = columns )
481
- result , expected = self ._return_result_expected (df , 1000 , ** func_params )
485
+ result , expected = self ._return_result_expected (
486
+ df , 1000 , temp_file , ** func_params
487
+ )
482
488
tm .assert_frame_equal (result , expected , check_names = False )
483
489
484
490
def test_to_csv_from_csv_w_some_infs (self , temp_file , float_frame ):
@@ -595,108 +601,104 @@ def test_to_csv_multiindex(self, temp_file, float_frame, datetime_frame):
595
601
# needed if setUp becomes class method
596
602
datetime_frame .index = old_index
597
603
598
- with tm .ensure_clean ("__tmp_to_csv_multiindex__" ) as path :
599
- # GH3571, GH1651, GH3141
600
-
601
- def _make_frame (names = None ):
602
- if names is True :
603
- names = ["first" , "second" ]
604
- return DataFrame (
605
- np .random .default_rng (2 ).integers (0 , 10 , size = (3 , 3 )),
606
- columns = MultiIndex .from_tuples (
607
- [("bah" , "foo" ), ("bah" , "bar" ), ("ban" , "baz" )], names = names
608
- ),
609
- dtype = "int64" ,
610
- )
611
-
612
- # column & index are multi-index
613
- df = DataFrame (
614
- np .ones ((5 , 3 )),
615
- columns = MultiIndex .from_arrays (
616
- [[f"i-{ i } " for i in range (3 )] for _ in range (4 )], names = list ("abcd" )
617
- ),
618
- index = MultiIndex .from_arrays (
619
- [[f"i-{ i } " for i in range (5 )] for _ in range (2 )], names = list ("ab" )
620
- ),
621
- )
622
- df .to_csv (path )
623
- result = read_csv (path , header = [0 , 1 , 2 , 3 ], index_col = [0 , 1 ])
624
- tm .assert_frame_equal (df , result )
625
-
626
- # column is mi
627
- df = DataFrame (
628
- np .ones ((5 , 3 )),
629
- columns = MultiIndex .from_arrays (
630
- [[f"i-{ i } " for i in range (3 )] for _ in range (4 )], names = list ("abcd" )
604
+ def _make_frame (names = None ):
605
+ if names is True :
606
+ names = ["first" , "second" ]
607
+ return DataFrame (
608
+ np .random .default_rng (2 ).integers (0 , 10 , size = (3 , 3 )),
609
+ columns = MultiIndex .from_tuples (
610
+ [("bah" , "foo" ), ("bah" , "bar" ), ("ban" , "baz" )], names = names
631
611
),
612
+ dtype = "int64" ,
632
613
)
633
- df .to_csv (path )
634
- result = read_csv (path , header = [0 , 1 , 2 , 3 ], index_col = 0 )
635
- tm .assert_frame_equal (df , result )
636
-
637
- # dup column names?
638
- df = DataFrame (
639
- np .ones ((5 , 3 )),
640
- columns = MultiIndex .from_arrays (
641
- [[f"i-{ i } " for i in range (3 )] for _ in range (4 )], names = list ("abcd" )
642
- ),
643
- index = MultiIndex .from_arrays (
644
- [[f"i-{ i } " for i in range (5 )] for _ in range (3 )], names = list ("abc" )
645
- ),
646
- )
647
- df .to_csv (path )
648
- result = read_csv (path , header = [0 , 1 , 2 , 3 ], index_col = [0 , 1 , 2 ])
649
- tm .assert_frame_equal (df , result )
650
-
651
- # writing with no index
652
- df = _make_frame ()
653
- df .to_csv (path , index = False )
654
- result = read_csv (path , header = [0 , 1 ])
655
- tm .assert_frame_equal (df , result )
656
-
657
- # we lose the names here
658
- df = _make_frame (True )
659
- df .to_csv (path , index = False )
660
- result = read_csv (path , header = [0 , 1 ])
661
- assert com .all_none (* result .columns .names )
662
- result .columns .names = df .columns .names
663
- tm .assert_frame_equal (df , result )
664
-
665
- # whatsnew example
666
- df = _make_frame ()
667
- df .to_csv (path )
668
- result = read_csv (path , header = [0 , 1 ], index_col = [0 ])
669
- tm .assert_frame_equal (df , result )
670
-
671
- df = _make_frame (True )
672
- df .to_csv (path )
673
- result = read_csv (path , header = [0 , 1 ], index_col = [0 ])
674
- tm .assert_frame_equal (df , result )
675
-
676
- # invalid options
677
- df = _make_frame (True )
678
- df .to_csv (path )
679
-
680
- for i in [6 , 7 ]:
681
- msg = f"len of { i } , but only 5 lines in file"
682
- with pytest .raises (ParserError , match = msg ):
683
- read_csv (path , header = list (range (i )), index_col = 0 )
684
-
685
- # write with cols
686
- msg = "cannot specify cols with a MultiIndex"
687
- with pytest .raises (TypeError , match = msg ):
688
- df .to_csv (path , columns = ["foo" , "bar" ])
689
-
690
- with tm .ensure_clean ("__tmp_to_csv_multiindex__" ) as path :
691
- # empty
692
- tsframe [:0 ].to_csv (path )
693
- recons = self .read_csv (path )
694
-
695
- exp = tsframe [:0 ]
696
- exp .index = []
697
-
698
- tm .assert_index_equal (recons .columns , exp .columns )
699
- assert len (recons ) == 0
614
+
615
+ # column & index are multi-index
616
+ df = DataFrame (
617
+ np .ones ((5 , 3 )),
618
+ columns = MultiIndex .from_arrays (
619
+ [[f"i-{ i } " for i in range (3 )] for _ in range (4 )], names = list ("abcd" )
620
+ ),
621
+ index = MultiIndex .from_arrays (
622
+ [[f"i-{ i } " for i in range (5 )] for _ in range (2 )], names = list ("ab" )
623
+ ),
624
+ )
625
+ df .to_csv (temp_file )
626
+ result = read_csv (temp_file , header = [0 , 1 , 2 , 3 ], index_col = [0 , 1 ])
627
+ tm .assert_frame_equal (df , result )
628
+
629
+ # column is mi
630
+ df = DataFrame (
631
+ np .ones ((5 , 3 )),
632
+ columns = MultiIndex .from_arrays (
633
+ [[f"i-{ i } " for i in range (3 )] for _ in range (4 )], names = list ("abcd" )
634
+ ),
635
+ )
636
+ df .to_csv (temp_file )
637
+ result = read_csv (temp_file , header = [0 , 1 , 2 , 3 ], index_col = 0 )
638
+ tm .assert_frame_equal (df , result )
639
+
640
+ # dup column names?
641
+ df = DataFrame (
642
+ np .ones ((5 , 3 )),
643
+ columns = MultiIndex .from_arrays (
644
+ [[f"i-{ i } " for i in range (3 )] for _ in range (4 )], names = list ("abcd" )
645
+ ),
646
+ index = MultiIndex .from_arrays (
647
+ [[f"i-{ i } " for i in range (5 )] for _ in range (3 )], names = list ("abc" )
648
+ ),
649
+ )
650
+ df .to_csv (temp_file )
651
+ result = read_csv (temp_file , header = [0 , 1 , 2 , 3 ], index_col = [0 , 1 , 2 ])
652
+ tm .assert_frame_equal (df , result )
653
+
654
+ # writing with no index
655
+ df = _make_frame ()
656
+ df .to_csv (temp_file , index = False )
657
+ result = read_csv (temp_file , header = [0 , 1 ])
658
+ tm .assert_frame_equal (df , result )
659
+
660
+ # we lose the names here
661
+ df = _make_frame (True )
662
+ df .to_csv (temp_file , index = False )
663
+ result = read_csv (temp_file , header = [0 , 1 ])
664
+ assert com .all_none (* result .columns .names )
665
+ result .columns .names = df .columns .names
666
+ tm .assert_frame_equal (df , result )
667
+
668
+ # whatsnew example
669
+ df = _make_frame ()
670
+ df .to_csv (temp_file )
671
+ result = read_csv (temp_file , header = [0 , 1 ], index_col = [0 ])
672
+ tm .assert_frame_equal (df , result )
673
+
674
+ df = _make_frame (True )
675
+ df .to_csv (temp_file )
676
+ result = read_csv (temp_file , header = [0 , 1 ], index_col = [0 ])
677
+ tm .assert_frame_equal (df , result )
678
+
679
+ # invalid options
680
+ df = _make_frame (True )
681
+ df .to_csv (temp_file )
682
+
683
+ for i in [6 , 7 ]:
684
+ msg = f"len of { i } , but only 5 lines in file"
685
+ with pytest .raises (ParserError , match = msg ):
686
+ read_csv (temp_file , header = list (range (i )), index_col = 0 )
687
+
688
+ # write with cols
689
+ msg = "cannot specify cols with a MultiIndex"
690
+ with pytest .raises (TypeError , match = msg ):
691
+ df .to_csv (temp_file , columns = ["foo" , "bar" ])
692
+
693
+ # empty
694
+ tsframe [:0 ].to_csv (temp_file )
695
+ recons = self .read_csv (temp_file )
696
+
697
+ exp = tsframe [:0 ]
698
+ exp .index = []
699
+
700
+ tm .assert_index_equal (recons .columns , exp .columns )
701
+ assert len (recons ) == 0
700
702
701
703
def test_to_csv_interval_index (self , temp_file , using_infer_string ):
702
704
# GH 28210
@@ -808,16 +810,15 @@ def test_to_csv_dups_cols(self, temp_file):
808
810
809
811
df .columns = [0 , 1 , 2 ] * 5
810
812
811
- with tm .ensure_clean () as filename :
812
- df .to_csv (filename )
813
- result = read_csv (filename , index_col = 0 )
813
+ df .to_csv (temp_file )
814
+ result = read_csv (temp_file , index_col = 0 )
814
815
815
- # date cols
816
- for i in ["0.4" , "1.4" , "2.4" ]:
817
- result [i ] = to_datetime (result [i ])
816
+ # date cols
817
+ for i in ["0.4" , "1.4" , "2.4" ]:
818
+ result [i ] = to_datetime (result [i ])
818
819
819
- result .columns = df .columns
820
- tm .assert_frame_equal (result , df )
820
+ result .columns = df .columns
821
+ tm .assert_frame_equal (result , df )
821
822
822
823
def test_to_csv_dups_cols2 (self , temp_file ):
823
824
# GH3457
@@ -1197,18 +1198,16 @@ def test_to_csv_with_dst_transitions_with_pickle(self, start, end, temp_file):
1197
1198
idx = idx ._with_freq (None ) # freq does not round-trip
1198
1199
idx ._data ._freq = None # otherwise there is trouble on unpickle
1199
1200
df = DataFrame ({"values" : 1 , "idx" : idx }, index = idx )
1200
- with tm .ensure_clean ("csv_date_format_with_dst" ) as path :
1201
- df .to_csv (path , index = True )
1202
- result = read_csv (path , index_col = 0 )
1203
- result .index = (
1204
- to_datetime (result .index , utc = True )
1205
- .tz_convert ("Europe/Paris" )
1206
- .as_unit ("ns" )
1207
- )
1208
- result ["idx" ] = to_datetime (result ["idx" ], utc = True ).astype (
1209
- "datetime64[ns, Europe/Paris]"
1210
- )
1211
- tm .assert_frame_equal (result , df )
1201
+
1202
+ df .to_csv (temp_file , index = True )
1203
+ result = read_csv (temp_file , index_col = 0 )
1204
+ result .index = (
1205
+ to_datetime (result .index , utc = True ).tz_convert ("Europe/Paris" ).as_unit ("ns" )
1206
+ )
1207
+ result ["idx" ] = to_datetime (result ["idx" ], utc = True ).astype (
1208
+ "datetime64[ns, Europe/Paris]"
1209
+ )
1210
+ tm .assert_frame_equal (result , df )
1212
1211
1213
1212
# assert working
1214
1213
df .astype (str )
0 commit comments