31
31
32
32
class TestDataFrameToCSV (TestData ):
33
33
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
+
34
40
def test_to_csv_from_csv1 (self ):
35
41
36
42
with ensure_clean ('__tmp_to_csv_from_csv1__' ) as path :
@@ -43,36 +49,31 @@ def test_to_csv_from_csv1(self):
43
49
44
50
# test roundtrip
45
51
self .tsframe .to_csv (path )
52
+ recons = self .read_csv (path )
53
+ assert_frame_equal (self .tsframe , recons )
46
54
47
55
with tm .assert_produces_warning (FutureWarning ,
48
56
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 )
51
59
52
60
self .tsframe .to_csv (path , index_label = 'index' )
61
+ recons = self .read_csv (path , index_col = None )
53
62
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 )
58
64
59
65
# no index
60
66
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 )
66
69
67
70
# corner case
68
71
dm = DataFrame ({'s1' : Series (lrange (3 ), lrange (3 )),
69
72
's2' : Series (lrange (2 ), lrange (2 ))})
70
73
dm .to_csv (path )
71
74
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 )
76
77
77
78
def test_to_csv_from_csv2 (self ):
78
79
@@ -82,36 +83,27 @@ def test_to_csv_from_csv2(self):
82
83
df = DataFrame (np .random .randn (3 , 3 ), index = ['a' , 'a' , 'b' ],
83
84
columns = ['x' , 'y' , 'z' ])
84
85
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 )
90
88
91
89
midx = MultiIndex .from_tuples (
92
90
[('A' , 1 , 2 ), ('A' , 1 , 2 ), ('B' , 1 , 2 )])
93
91
df = DataFrame (np .random .randn (3 , 3 ), index = midx ,
94
92
columns = ['x' , 'y' , 'z' ])
95
- df .to_csv (path )
96
93
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 )
103
97
assert_frame_equal (result , df , check_names = False )
104
98
105
99
# column aliases
106
100
col_aliases = Index (['AA' , 'X' , 'Y' , 'Z' ])
107
101
self .frame2 .to_csv (path , header = col_aliases )
108
102
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 )
115
107
116
108
pytest .raises (ValueError , self .frame2 .to_csv , path ,
117
109
header = ['AA' , 'X' ])
@@ -251,12 +243,10 @@ def make_dtnat_arr(n, nnat=None):
251
243
df = DataFrame (dict (a = s1 , b = s2 ))
252
244
df .to_csv (pth , chunksize = chunksize )
253
245
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 )
260
250
261
251
@pytest .mark .slow
262
252
def test_to_csv_moar (self ):
@@ -269,22 +259,17 @@ def _do_test(df, r_dtype=None, c_dtype=None,
269
259
if rnlvl is not None :
270
260
kwargs ['index_col' ] = lrange (rnlvl )
271
261
kwargs ['header' ] = lrange (cnlvl )
262
+
272
263
with ensure_clean ('__tmp_to_csv_moar__' ) as path :
273
264
df .to_csv (path , encoding = 'utf8' ,
274
265
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 )
280
267
else :
281
268
kwargs ['header' ] = 0
269
+
282
270
with ensure_clean ('__tmp_to_csv_moar__' ) as path :
283
271
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 )
288
273
289
274
def _to_uni (x ):
290
275
if not isinstance (x , compat .text_type ):
@@ -426,15 +411,12 @@ def test_to_csv_from_csv_w_some_infs(self):
426
411
427
412
with ensure_clean () as path :
428
413
self .frame .to_csv (path )
414
+ recons = self .read_csv (path )
429
415
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 )
438
420
439
421
def test_to_csv_from_csv_w_all_infs (self ):
440
422
@@ -444,15 +426,12 @@ def test_to_csv_from_csv_w_all_infs(self):
444
426
445
427
with ensure_clean () as path :
446
428
self .frame .to_csv (path )
429
+ recons = self .read_csv (path )
447
430
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 )
456
435
457
436
def test_to_csv_no_index (self ):
458
437
# GH 3624, after appending columns, to_csv fails
@@ -482,19 +461,15 @@ def test_to_csv_headers(self):
482
461
to_df = DataFrame ([[1 , 2 ], [3 , 4 ]], columns = ['X' , 'Y' ])
483
462
with ensure_clean ('__tmp_to_csv_headers__' ) as path :
484
463
from_df .to_csv (path , header = ['X' , 'Y' ])
464
+ recons = self .read_csv (path )
485
465
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 )
490
467
491
468
from_df .to_csv (path , index = False , header = ['X' , 'Y' ])
469
+ recons = self .read_csv (path )
492
470
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 )
498
473
499
474
def test_to_csv_multiindex (self ):
500
475
@@ -512,16 +487,14 @@ def test_to_csv_multiindex(self):
512
487
# round trip
513
488
frame .to_csv (path )
514
489
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 )
519
492
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
523
496
524
- # needed if setUP becomes a classmethod
497
+ # needed if setUp becomes a class method
525
498
self .frame .index = old_index
526
499
527
500
# try multiindex with dates
@@ -531,30 +504,22 @@ def test_to_csv_multiindex(self):
531
504
tsframe .index = MultiIndex .from_arrays (new_index )
532
505
533
506
tsframe .to_csv (path , index_label = ['time' , 'foo' ])
507
+ recons = self .read_csv (path , index_col = [0 , 1 ])
534
508
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 )
540
511
541
512
# do not load index
542
513
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
548
516
549
517
# no index
550
518
tsframe .to_csv (path , index = False )
519
+ recons = self .read_csv (path , index_col = None )
520
+ assert_almost_equal (recons .values , self .tsframe .values )
551
521
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
558
523
self .tsframe .index = old_index
559
524
560
525
with ensure_clean ('__tmp_to_csv_multiindex__' ) as path :
@@ -652,15 +617,13 @@ def _make_frame(names=None):
652
617
with ensure_clean ('__tmp_to_csv_multiindex__' ) as path :
653
618
# empty
654
619
tsframe [:0 ].to_csv (path )
620
+ recons = self .read_csv (path )
655
621
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 = []
661
624
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
664
627
665
628
def test_to_csv_float32_nanrep (self ):
666
629
df = DataFrame (np .random .randn (1 , 4 ).astype (np .float32 ))
@@ -680,11 +643,8 @@ def test_to_csv_withcommas(self):
680
643
681
644
with ensure_clean ('__tmp_to_csv_withcommas__.csv' ) as path :
682
645
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 )
688
648
689
649
def test_to_csv_mixed (self ):
690
650
@@ -798,11 +758,8 @@ def test_to_csv_wide_frame_formatting(self):
798
758
799
759
def test_to_csv_bug (self ):
800
760
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 ]]})
806
763
807
764
with ensure_clean () as path :
808
765
newdf .to_csv (path )
0 commit comments