3131
3232class TestDataFrameToCSV (TestData ):
3333
34+ def read_csv (self , path , ** kwargs ):
35+ params = dict (index_col = 0 , parse_dates = True )
36+ params .update (** kwargs )
37+
38+ return pd .read_csv (path , ** params )
39+
3440 def test_to_csv_from_csv1 (self ):
3541
3642 with ensure_clean ('__tmp_to_csv_from_csv1__' ) as path :
@@ -43,36 +49,31 @@ def test_to_csv_from_csv1(self):
4349
4450 # test roundtrip
4551 self .tsframe .to_csv (path )
52+ recons = self .read_csv (path )
53+ assert_frame_equal (self .tsframe , recons )
4654
4755 with tm .assert_produces_warning (FutureWarning ,
4856 check_stacklevel = False ):
49- recons = DataFrame .from_csv (path )
50- assert_frame_equal (self .tsframe , recons )
57+ depr_recons = DataFrame .from_csv (path )
58+ assert_frame_equal (self .tsframe , depr_recons )
5159
5260 self .tsframe .to_csv (path , index_label = 'index' )
61+ recons = self .read_csv (path , index_col = None )
5362
54- with tm .assert_produces_warning (FutureWarning ,
55- check_stacklevel = False ):
56- recons = DataFrame .from_csv (path , index_col = None )
57- assert (len (recons .columns ) == len (self .tsframe .columns ) + 1 )
63+ assert (len (recons .columns ) == len (self .tsframe .columns ) + 1 )
5864
5965 # no index
6066 self .tsframe .to_csv (path , index = False )
61-
62- with tm .assert_produces_warning (FutureWarning ,
63- check_stacklevel = False ):
64- recons = DataFrame .from_csv (path , index_col = None )
65- assert_almost_equal (self .tsframe .values , recons .values )
67+ recons = self .read_csv (path , index_col = None )
68+ assert_almost_equal (self .tsframe .values , recons .values )
6669
6770 # corner case
6871 dm = DataFrame ({'s1' : Series (lrange (3 ), lrange (3 )),
6972 's2' : Series (lrange (2 ), lrange (2 ))})
7073 dm .to_csv (path )
7174
72- with tm .assert_produces_warning (FutureWarning ,
73- check_stacklevel = False ):
74- recons = DataFrame .from_csv (path )
75- assert_frame_equal (dm , recons )
75+ recons = self .read_csv (path )
76+ assert_frame_equal (dm , recons )
7677
7778 def test_to_csv_from_csv2 (self ):
7879
@@ -82,36 +83,27 @@ def test_to_csv_from_csv2(self):
8283 df = DataFrame (np .random .randn (3 , 3 ), index = ['a' , 'a' , 'b' ],
8384 columns = ['x' , 'y' , 'z' ])
8485 df .to_csv (path )
85-
86- with tm .assert_produces_warning (FutureWarning ,
87- check_stacklevel = False ):
88- result = DataFrame .from_csv (path )
89- assert_frame_equal (result , df )
86+ result = self .read_csv (path )
87+ assert_frame_equal (result , df )
9088
9189 midx = MultiIndex .from_tuples (
9290 [('A' , 1 , 2 ), ('A' , 1 , 2 ), ('B' , 1 , 2 )])
9391 df = DataFrame (np .random .randn (3 , 3 ), index = midx ,
9492 columns = ['x' , 'y' , 'z' ])
95- df .to_csv (path )
9693
97- with tm .assert_produces_warning (FutureWarning ,
98- check_stacklevel = False ):
99- result = DataFrame .from_csv (path , index_col = [0 , 1 , 2 ],
100- parse_dates = False )
101- # TODO from_csv names index ['Unnamed: 1', 'Unnamed: 2'] should it
102- # ?
94+ df .to_csv (path )
95+ result = self .read_csv (path , index_col = [0 , 1 , 2 ],
96+ parse_dates = False )
10397 assert_frame_equal (result , df , check_names = False )
10498
10599 # column aliases
106100 col_aliases = Index (['AA' , 'X' , 'Y' , 'Z' ])
107101 self .frame2 .to_csv (path , header = col_aliases )
108102
109- with tm .assert_produces_warning (FutureWarning ,
110- check_stacklevel = False ):
111- rs = DataFrame .from_csv (path )
112- xp = self .frame2 .copy ()
113- xp .columns = col_aliases
114- assert_frame_equal (xp , rs )
103+ rs = self .read_csv (path )
104+ xp = self .frame2 .copy ()
105+ xp .columns = col_aliases
106+ assert_frame_equal (xp , rs )
115107
116108 pytest .raises (ValueError , self .frame2 .to_csv , path ,
117109 header = ['AA' , 'X' ])
@@ -251,12 +243,10 @@ def make_dtnat_arr(n, nnat=None):
251243 df = DataFrame (dict (a = s1 , b = s2 ))
252244 df .to_csv (pth , chunksize = chunksize )
253245
254- with tm .assert_produces_warning (FutureWarning ,
255- check_stacklevel = False ):
256- recons = DataFrame .from_csv (pth )._convert (datetime = True ,
257- coerce = True )
258- assert_frame_equal (df , recons , check_names = False ,
259- check_less_precise = True )
246+ recons = self .read_csv (pth )._convert (datetime = True ,
247+ coerce = True )
248+ assert_frame_equal (df , recons , check_names = False ,
249+ check_less_precise = True )
260250
261251 @pytest .mark .slow
262252 def test_to_csv_moar (self ):
@@ -269,22 +259,17 @@ def _do_test(df, r_dtype=None, c_dtype=None,
269259 if rnlvl is not None :
270260 kwargs ['index_col' ] = lrange (rnlvl )
271261 kwargs ['header' ] = lrange (cnlvl )
262+
272263 with ensure_clean ('__tmp_to_csv_moar__' ) as path :
273264 df .to_csv (path , encoding = 'utf8' ,
274265 chunksize = chunksize , tupleize_cols = False )
275-
276- with tm .assert_produces_warning (FutureWarning ,
277- check_stacklevel = False ):
278- recons = DataFrame .from_csv (
279- path , tupleize_cols = False , ** kwargs )
266+ recons = self .read_csv (path , tupleize_cols = False , ** kwargs )
280267 else :
281268 kwargs ['header' ] = 0
269+
282270 with ensure_clean ('__tmp_to_csv_moar__' ) as path :
283271 df .to_csv (path , encoding = 'utf8' , chunksize = chunksize )
284-
285- with tm .assert_produces_warning (FutureWarning ,
286- check_stacklevel = False ):
287- recons = DataFrame .from_csv (path , ** kwargs )
272+ recons = self .read_csv (path , ** kwargs )
288273
289274 def _to_uni (x ):
290275 if not isinstance (x , compat .text_type ):
@@ -426,15 +411,12 @@ def test_to_csv_from_csv_w_some_infs(self):
426411
427412 with ensure_clean () as path :
428413 self .frame .to_csv (path )
414+ recons = self .read_csv (path )
429415
430- with tm .assert_produces_warning (FutureWarning ,
431- check_stacklevel = False ):
432- recons = DataFrame .from_csv (path )
433-
434- # TODO to_csv drops column name
435- assert_frame_equal (self .frame , recons , check_names = False )
436- assert_frame_equal (np .isinf (self .frame ),
437- np .isinf (recons ), check_names = False )
416+ # TODO to_csv drops column name
417+ assert_frame_equal (self .frame , recons , check_names = False )
418+ assert_frame_equal (np .isinf (self .frame ),
419+ np .isinf (recons ), check_names = False )
438420
439421 def test_to_csv_from_csv_w_all_infs (self ):
440422
@@ -444,15 +426,12 @@ def test_to_csv_from_csv_w_all_infs(self):
444426
445427 with ensure_clean () as path :
446428 self .frame .to_csv (path )
429+ recons = self .read_csv (path )
447430
448- with tm .assert_produces_warning (FutureWarning ,
449- check_stacklevel = False ):
450- recons = DataFrame .from_csv (path )
451-
452- # TODO to_csv drops column name
453- assert_frame_equal (self .frame , recons , check_names = False )
454- assert_frame_equal (np .isinf (self .frame ),
455- np .isinf (recons ), check_names = False )
431+ # TODO to_csv drops column name
432+ assert_frame_equal (self .frame , recons , check_names = False )
433+ assert_frame_equal (np .isinf (self .frame ),
434+ np .isinf (recons ), check_names = False )
456435
457436 def test_to_csv_no_index (self ):
458437 # GH 3624, after appending columns, to_csv fails
@@ -482,19 +461,15 @@ def test_to_csv_headers(self):
482461 to_df = DataFrame ([[1 , 2 ], [3 , 4 ]], columns = ['X' , 'Y' ])
483462 with ensure_clean ('__tmp_to_csv_headers__' ) as path :
484463 from_df .to_csv (path , header = ['X' , 'Y' ])
464+ recons = self .read_csv (path )
485465
486- with tm .assert_produces_warning (FutureWarning ,
487- check_stacklevel = False ):
488- recons = DataFrame .from_csv (path )
489- assert_frame_equal (to_df , recons )
466+ assert_frame_equal (to_df , recons )
490467
491468 from_df .to_csv (path , index = False , header = ['X' , 'Y' ])
469+ recons = self .read_csv (path )
492470
493- with tm .assert_produces_warning (FutureWarning ,
494- check_stacklevel = False ):
495- recons = DataFrame .from_csv (path )
496- recons .reset_index (inplace = True )
497- assert_frame_equal (to_df , recons )
471+ recons .reset_index (inplace = True )
472+ assert_frame_equal (to_df , recons )
498473
499474 def test_to_csv_multiindex (self ):
500475
@@ -512,16 +487,14 @@ def test_to_csv_multiindex(self):
512487 # round trip
513488 frame .to_csv (path )
514489
515- with tm .assert_produces_warning (FutureWarning ,
516- check_stacklevel = False ):
517- df = DataFrame .from_csv (path , index_col = [0 , 1 ],
518- parse_dates = False )
490+ df = self .read_csv (path , index_col = [0 , 1 ],
491+ parse_dates = False )
519492
520- # TODO to_csv drops column name
521- assert_frame_equal (frame , df , check_names = False )
522- assert frame .index .names == df .index .names
493+ # TODO to_csv drops column name
494+ assert_frame_equal (frame , df , check_names = False )
495+ assert frame .index .names == df .index .names
523496
524- # needed if setUP becomes a classmethod
497+ # needed if setUp becomes a class method
525498 self .frame .index = old_index
526499
527500 # try multiindex with dates
@@ -531,30 +504,22 @@ def test_to_csv_multiindex(self):
531504 tsframe .index = MultiIndex .from_arrays (new_index )
532505
533506 tsframe .to_csv (path , index_label = ['time' , 'foo' ])
507+ recons = self .read_csv (path , index_col = [0 , 1 ])
534508
535- with tm .assert_produces_warning (FutureWarning ,
536- check_stacklevel = False ):
537- recons = DataFrame .from_csv (path , index_col = [0 , 1 ])
538- # TODO to_csv drops column name
539- assert_frame_equal (tsframe , recons , check_names = False )
509+ # TODO to_csv drops column name
510+ assert_frame_equal (tsframe , recons , check_names = False )
540511
541512 # do not load index
542513 tsframe .to_csv (path )
543-
544- with tm .assert_produces_warning (FutureWarning ,
545- check_stacklevel = False ):
546- recons = DataFrame .from_csv (path , index_col = None )
547- assert len (recons .columns ) == len (tsframe .columns ) + 2
514+ recons = self .read_csv (path , index_col = None )
515+ assert len (recons .columns ) == len (tsframe .columns ) + 2
548516
549517 # no index
550518 tsframe .to_csv (path , index = False )
519+ recons = self .read_csv (path , index_col = None )
520+ assert_almost_equal (recons .values , self .tsframe .values )
551521
552- with tm .assert_produces_warning (FutureWarning ,
553- check_stacklevel = False ):
554- recons = DataFrame .from_csv (path , index_col = None )
555- assert_almost_equal (recons .values , self .tsframe .values )
556-
557- # needed if setUP becomes classmethod
522+ # needed if setUp becomes class method
558523 self .tsframe .index = old_index
559524
560525 with ensure_clean ('__tmp_to_csv_multiindex__' ) as path :
@@ -652,15 +617,13 @@ def _make_frame(names=None):
652617 with ensure_clean ('__tmp_to_csv_multiindex__' ) as path :
653618 # empty
654619 tsframe [:0 ].to_csv (path )
620+ recons = self .read_csv (path )
655621
656- with tm .assert_produces_warning (FutureWarning ,
657- check_stacklevel = False ):
658- recons = DataFrame .from_csv (path )
659- exp = tsframe [:0 ]
660- exp .index = []
622+ exp = tsframe [:0 ]
623+ exp .index = []
661624
662- tm .assert_index_equal (recons .columns , exp .columns )
663- assert len (recons ) == 0
625+ tm .assert_index_equal (recons .columns , exp .columns )
626+ assert len (recons ) == 0
664627
665628 def test_to_csv_float32_nanrep (self ):
666629 df = DataFrame (np .random .randn (1 , 4 ).astype (np .float32 ))
@@ -680,11 +643,8 @@ def test_to_csv_withcommas(self):
680643
681644 with ensure_clean ('__tmp_to_csv_withcommas__.csv' ) as path :
682645 df .to_csv (path )
683-
684- with tm .assert_produces_warning (FutureWarning ,
685- check_stacklevel = False ):
686- df2 = DataFrame .from_csv (path )
687- assert_frame_equal (df2 , df )
646+ df2 = self .read_csv (path )
647+ assert_frame_equal (df2 , df )
688648
689649 def test_to_csv_mixed (self ):
690650
@@ -798,11 +758,8 @@ def test_to_csv_wide_frame_formatting(self):
798758
799759 def test_to_csv_bug (self ):
800760 f1 = StringIO ('a,1.0\n b,2.0' )
801-
802- with tm .assert_produces_warning (FutureWarning ,
803- check_stacklevel = False ):
804- df = DataFrame .from_csv (f1 , header = None )
805- newdf = DataFrame ({'t' : df [df .columns [0 ]]})
761+ df = self .read_csv (f1 , header = None )
762+ newdf = DataFrame ({'t' : df [df .columns [0 ]]})
806763
807764 with ensure_clean () as path :
808765 newdf .to_csv (path )
0 commit comments