@@ -261,14 +261,13 @@ def _return_result_expected(
261
261
kwargs ["index_col" ] = list (range (rnlvl ))
262
262
kwargs ["header" ] = list (range (cnlvl ))
263
263
264
-
265
264
path = str (temp_file )
266
265
df .to_csv (path , encoding = "utf8" , chunksize = chunksize )
267
266
recons = self .read_csv (path , ** kwargs )
268
267
else :
269
268
kwargs ["header" ] = 0
270
269
271
- path = str (temp_file )
270
+ path = str (temp_file )
272
271
df .to_csv (path , encoding = "utf8" , chunksize = chunksize )
273
272
recons = self .read_csv (path , ** kwargs )
274
273
@@ -430,7 +429,9 @@ def test_to_csv_dup_cols(self, nrows, temp_file):
430
429
ix [- 2 :] = ["rdupe" , "rdupe" ]
431
430
df .index = ix
432
431
df .columns = cols
433
- result , expected = self ._return_result_expected (df , 1000 , temp_file , dupe_col = True )
432
+ result , expected = self ._return_result_expected (
433
+ df , 1000 , temp_file , dupe_col = True
434
+ )
434
435
tm .assert_frame_equal (result , expected , check_names = False )
435
436
436
437
@pytest .mark .slow
@@ -448,7 +449,9 @@ def test_to_csv_chunksize(self, temp_file):
448
449
columns = Index (list ("ab" )),
449
450
index = MultiIndex .from_arrays ([range (rows ) for _ in range (2 )]),
450
451
)
451
- result , expected = self ._return_result_expected (df , chunksize , temp_file , rnlvl = 2 )
452
+ result , expected = self ._return_result_expected (
453
+ df , chunksize , temp_file , rnlvl = 2
454
+ )
452
455
tm .assert_frame_equal (result , expected , check_names = False )
453
456
454
457
@pytest .mark .slow
@@ -481,7 +484,9 @@ def test_to_csv_params(self, nrows, df_params, func_params, ncols, temp_file):
481
484
else :
482
485
columns = Index ([f"i-{ i } " for i in range (ncols )])
483
486
df = DataFrame (np .ones ((nrows , ncols )), index = index , columns = columns )
484
- result , expected = self ._return_result_expected (df , 1000 , temp_file , ** func_params )
487
+ result , expected = self ._return_result_expected (
488
+ df , 1000 , temp_file , ** func_params
489
+ )
485
490
tm .assert_frame_equal (result , expected , check_names = False )
486
491
487
492
def test_to_csv_from_csv_w_some_infs (self , temp_file , float_frame ):
@@ -598,108 +603,104 @@ def test_to_csv_multiindex(self, temp_file, float_frame, datetime_frame):
598
603
# needed if setUp becomes class method
599
604
datetime_frame .index = old_index
600
605
601
- with tm .ensure_clean ("__tmp_to_csv_multiindex__" ) as path :
602
- # GH3571, GH1651, GH3141
603
-
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
611
- ),
612
- dtype = "int64" ,
613
- )
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 (path )
626
- result = read_csv (path , 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" )
606
+ def _make_frame (names = None ):
607
+ if names is True :
608
+ names = ["first" , "second" ]
609
+ return DataFrame (
610
+ np .random .default_rng (2 ).integers (0 , 10 , size = (3 , 3 )),
611
+ columns = MultiIndex .from_tuples (
612
+ [("bah" , "foo" ), ("bah" , "bar" ), ("ban" , "baz" )], names = names
634
613
),
614
+ dtype = "int64" ,
635
615
)
636
- df .to_csv (path )
637
- result = read_csv (path , 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 (path )
651
- result = read_csv (path , 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 (path , index = False )
657
- result = read_csv (path , 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 (path , index = False )
663
- result = read_csv (path , 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 (path )
671
- result = read_csv (path , header = [0 , 1 ], index_col = [0 ])
672
- tm .assert_frame_equal (df , result )
673
-
674
- df = _make_frame (True )
675
- df .to_csv (path )
676
- result = read_csv (path , 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 (path )
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 (path , 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 (path , columns = ["foo" , "bar" ])
692
-
693
- with tm .ensure_clean ("__tmp_to_csv_multiindex__" ) as path :
694
- # empty
695
- tsframe [:0 ].to_csv (path )
696
- recons = self .read_csv (path )
697
-
698
- exp = tsframe [:0 ]
699
- exp .index = []
700
-
701
- tm .assert_index_equal (recons .columns , exp .columns )
702
- assert len (recons ) == 0
616
+
617
+ # column & index are multi-index
618
+ df = DataFrame (
619
+ np .ones ((5 , 3 )),
620
+ columns = MultiIndex .from_arrays (
621
+ [[f"i-{ i } " for i in range (3 )] for _ in range (4 )], names = list ("abcd" )
622
+ ),
623
+ index = MultiIndex .from_arrays (
624
+ [[f"i-{ i } " for i in range (5 )] for _ in range (2 )], names = list ("ab" )
625
+ ),
626
+ )
627
+ df .to_csv (path )
628
+ result = read_csv (path , header = [0 , 1 , 2 , 3 ], index_col = [0 , 1 ])
629
+ tm .assert_frame_equal (df , result )
630
+
631
+ # column is mi
632
+ df = DataFrame (
633
+ np .ones ((5 , 3 )),
634
+ columns = MultiIndex .from_arrays (
635
+ [[f"i-{ i } " for i in range (3 )] for _ in range (4 )], names = list ("abcd" )
636
+ ),
637
+ )
638
+ df .to_csv (path )
639
+ result = read_csv (path , header = [0 , 1 , 2 , 3 ], index_col = 0 )
640
+ tm .assert_frame_equal (df , result )
641
+
642
+ # dup column names?
643
+ df = DataFrame (
644
+ np .ones ((5 , 3 )),
645
+ columns = MultiIndex .from_arrays (
646
+ [[f"i-{ i } " for i in range (3 )] for _ in range (4 )], names = list ("abcd" )
647
+ ),
648
+ index = MultiIndex .from_arrays (
649
+ [[f"i-{ i } " for i in range (5 )] for _ in range (3 )], names = list ("abc" )
650
+ ),
651
+ )
652
+ df .to_csv (path )
653
+ result = read_csv (path , header = [0 , 1 , 2 , 3 ], index_col = [0 , 1 , 2 ])
654
+ tm .assert_frame_equal (df , result )
655
+
656
+ # writing with no index
657
+ df = _make_frame ()
658
+ df .to_csv (path , index = False )
659
+ result = read_csv (path , header = [0 , 1 ])
660
+ tm .assert_frame_equal (df , result )
661
+
662
+ # we lose the names here
663
+ df = _make_frame (True )
664
+ df .to_csv (path , index = False )
665
+ result = read_csv (path , header = [0 , 1 ])
666
+ assert com .all_none (* result .columns .names )
667
+ result .columns .names = df .columns .names
668
+ tm .assert_frame_equal (df , result )
669
+
670
+ # whatsnew example
671
+ df = _make_frame ()
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
+ df = _make_frame (True )
677
+ df .to_csv (path )
678
+ result = read_csv (path , header = [0 , 1 ], index_col = [0 ])
679
+ tm .assert_frame_equal (df , result )
680
+
681
+ # invalid options
682
+ df = _make_frame (True )
683
+ df .to_csv (path )
684
+
685
+ for i in [6 , 7 ]:
686
+ msg = f"len of { i } , but only 5 lines in file"
687
+ with pytest .raises (ParserError , match = msg ):
688
+ read_csv (path , header = list (range (i )), index_col = 0 )
689
+
690
+ # write with cols
691
+ msg = "cannot specify cols with a MultiIndex"
692
+ with pytest .raises (TypeError , match = msg ):
693
+ df .to_csv (path , columns = ["foo" , "bar" ])
694
+
695
+ # empty
696
+ tsframe [:0 ].to_csv (path )
697
+ recons = self .read_csv (path )
698
+
699
+ exp = tsframe [:0 ]
700
+ exp .index = []
701
+
702
+ tm .assert_index_equal (recons .columns , exp .columns )
703
+ assert len (recons ) == 0
703
704
704
705
def test_to_csv_interval_index (self , temp_file , using_infer_string ):
705
706
# GH 28210
@@ -811,16 +812,15 @@ def test_to_csv_dups_cols(self, temp_file):
811
812
812
813
df .columns = [0 , 1 , 2 ] * 5
813
814
814
- with tm .ensure_clean () as filename :
815
- df .to_csv (filename )
816
- result = read_csv (filename , index_col = 0 )
815
+ df .to_csv (path )
816
+ result = read_csv (path , index_col = 0 )
817
817
818
- # date cols
819
- for i in ["0.4" , "1.4" , "2.4" ]:
820
- result [i ] = to_datetime (result [i ])
818
+ # date cols
819
+ for i in ["0.4" , "1.4" , "2.4" ]:
820
+ result [i ] = to_datetime (result [i ])
821
821
822
- result .columns = df .columns
823
- tm .assert_frame_equal (result , df )
822
+ result .columns = df .columns
823
+ tm .assert_frame_equal (result , df )
824
824
825
825
def test_to_csv_dups_cols2 (self , temp_file ):
826
826
# GH3457
@@ -1200,18 +1200,17 @@ def test_to_csv_with_dst_transitions_with_pickle(self, start, end, temp_file):
1200
1200
idx = idx ._with_freq (None ) # freq does not round-trip
1201
1201
idx ._data ._freq = None # otherwise there is trouble on unpickle
1202
1202
df = DataFrame ({"values" : 1 , "idx" : idx }, index = idx )
1203
- with tm .ensure_clean ("csv_date_format_with_dst" ) as path :
1204
- df .to_csv (path , index = True )
1205
- result = read_csv (path , index_col = 0 )
1206
- result .index = (
1207
- to_datetime (result .index , utc = True )
1208
- .tz_convert ("Europe/Paris" )
1209
- .as_unit ("ns" )
1210
- )
1211
- result ["idx" ] = to_datetime (result ["idx" ], utc = True ).astype (
1212
- "datetime64[ns, Europe/Paris]"
1213
- )
1214
- tm .assert_frame_equal (result , df )
1203
+
1204
+ path = str (temp_file )
1205
+ df .to_csv (path , index = True )
1206
+ result = read_csv (path , index_col = 0 )
1207
+ result .index = (
1208
+ to_datetime (result .index , utc = True ).tz_convert ("Europe/Paris" ).as_unit ("ns" )
1209
+ )
1210
+ result ["idx" ] = to_datetime (result ["idx" ], utc = True ).astype (
1211
+ "datetime64[ns, Europe/Paris]"
1212
+ )
1213
+ tm .assert_frame_equal (result , df )
1215
1214
1216
1215
# assert working
1217
1216
df .astype (str )
0 commit comments