20
20
21
21
class TestSeriesToCSV (TestData ):
22
22
23
+ def read_csv (self , path , ** kwargs ):
24
+ params = dict (squeeze = True , index_col = 0 ,
25
+ header = None , parse_dates = True )
26
+ params .update (** kwargs )
27
+
28
+ header = params .get ("header" )
29
+ out = pd .read_csv (path , ** params )
30
+
31
+ if header is None :
32
+ out .name = out .index .name = None
33
+
34
+ return out
35
+
23
36
def test_from_csv (self ):
24
37
25
38
with ensure_clean () as path :
26
- with tm .assert_produces_warning (FutureWarning ,
27
- check_stacklevel = False ):
28
- self .ts .to_csv (path )
29
- ts = Series .from_csv (path )
30
- assert_series_equal (self .ts , ts , check_names = False )
31
- assert ts .name is None
32
- assert ts .index .name is None
39
+ self .ts .to_csv (path )
40
+ ts = self .read_csv (path )
41
+ assert_series_equal (self .ts , ts , check_names = False )
33
42
34
- with tm .assert_produces_warning (FutureWarning ,
35
- check_stacklevel = False ):
36
- # GH10483
37
- self .ts .to_csv (path , header = True )
38
- ts_h = Series .from_csv (path , header = 0 )
39
- assert ts_h .name == 'ts'
43
+ assert ts .name is None
44
+ assert ts .index .name is None
40
45
41
46
with tm .assert_produces_warning (FutureWarning ,
42
47
check_stacklevel = False ):
43
- self .series .to_csv (path )
44
- series = Series .from_csv (path )
45
- assert series .name is None
46
- assert series .index .name is None
47
- assert_series_equal (self .series , series , check_names = False )
48
- assert series .name is None
49
- assert series .index .name is None
48
+ depr_ts = Series .from_csv (path )
49
+ assert_series_equal (depr_ts , ts )
50
50
51
- with tm .assert_produces_warning (FutureWarning ,
52
- check_stacklevel = False ):
53
- self .series .to_csv (path , header = True )
54
- series_h = Series .from_csv (path , header = 0 )
55
- assert series_h .name == 'series'
51
+ # see gh-10483
52
+ self .ts .to_csv (path , header = True )
53
+ ts_h = self .read_csv (path , header = 0 )
54
+ assert ts_h .name == "ts"
56
55
57
- with tm .assert_produces_warning (FutureWarning ,
58
- check_stacklevel = False ):
59
- outfile = open (path , 'w' )
60
- outfile .write ('1998-01-01|1.0\n 1999-01-01|2.0' )
61
- outfile .close ()
62
- series = Series .from_csv (path , sep = '|' )
63
- checkseries = Series ({datetime (1998 , 1 , 1 ): 1.0 ,
64
- datetime (1999 , 1 , 1 ): 2.0 })
65
- assert_series_equal (checkseries , series )
56
+ self .series .to_csv (path )
57
+ series = self .read_csv (path )
58
+ assert_series_equal (self .series , series , check_names = False )
66
59
67
- with tm .assert_produces_warning (FutureWarning ,
68
- check_stacklevel = False ):
69
- series = Series .from_csv (path , sep = '|' , parse_dates = False )
70
- checkseries = Series ({'1998-01-01' : 1.0 , '1999-01-01' : 2.0 })
71
- assert_series_equal (checkseries , series )
60
+ assert series .name is None
61
+ assert series .index .name is None
62
+
63
+ self .series .to_csv (path , header = True )
64
+ series_h = self .read_csv (path , header = 0 )
65
+ assert series_h .name == "series"
66
+
67
+ outfile = open (path , "w" )
68
+ outfile .write ("1998-01-01|1.0\n 1999-01-01|2.0" )
69
+ outfile .close ()
70
+
71
+ series = self .read_csv (path , sep = "|" )
72
+ check_series = Series ({datetime (1998 , 1 , 1 ): 1.0 ,
73
+ datetime (1999 , 1 , 1 ): 2.0 })
74
+ assert_series_equal (check_series , series )
75
+
76
+ series = self .read_csv (path , sep = "|" , parse_dates = False )
77
+ check_series = Series ({"1998-01-01" : 1.0 , "1999-01-01" : 2.0 })
78
+ assert_series_equal (check_series , series )
72
79
73
80
def test_to_csv (self ):
74
81
import io
@@ -88,25 +95,21 @@ def test_to_csv_unicode_index(self):
88
95
buf = StringIO ()
89
96
s = Series ([u ("\u05d0 " ), "d2" ], index = [u ("\u05d0 " ), u ("\u05d1 " )])
90
97
91
- s .to_csv (buf , encoding = ' UTF-8' )
98
+ s .to_csv (buf , encoding = " UTF-8" )
92
99
buf .seek (0 )
93
100
94
- with tm .assert_produces_warning (FutureWarning ,
95
- check_stacklevel = False ):
96
- s2 = Series .from_csv (buf , index_col = 0 , encoding = 'UTF-8' )
97
- assert_series_equal (s , s2 )
101
+ s2 = self .read_csv (buf , index_col = 0 , encoding = "UTF-8" )
102
+ assert_series_equal (s , s2 )
98
103
99
104
def test_to_csv_float_format (self ):
100
105
101
106
with ensure_clean () as filename :
102
107
ser = Series ([0.123456 , 0.234567 , 0.567567 ])
103
- ser .to_csv (filename , float_format = ' %.2f' )
108
+ ser .to_csv (filename , float_format = " %.2f" )
104
109
105
- with tm .assert_produces_warning (FutureWarning ,
106
- check_stacklevel = False ):
107
- rs = Series .from_csv (filename )
108
- xp = Series ([0.12 , 0.23 , 0.57 ])
109
- assert_series_equal (rs , xp )
110
+ rs = self .read_csv (filename )
111
+ xp = Series ([0.12 , 0.23 , 0.57 ])
112
+ assert_series_equal (rs , xp )
110
113
111
114
def test_to_csv_list_entries (self ):
112
115
s = Series (['jack and jill' , 'jesse and frank' ])
0 commit comments