6
6
import os
7
7
from os .path import isabs
8
8
9
+ import pandas as pd
9
10
import pandas .util .testing as tm
10
11
11
12
from pandas .io import common
24
25
pass
25
26
26
27
28
+ class CustomFSPath (object ):
29
+ """For testing fspath on unknown objects"""
30
+ def __init__ (self , path ):
31
+ self .path = path
32
+
33
+ def __fspath__ (self ):
34
+ return self .path
35
+
36
+
37
+ HERE = os .path .dirname (__file__ )
38
+
39
+
27
40
class TestCommonIOCapabilities (object ):
28
41
data1 = """index,A,B,C,D
29
42
foo,2,3,4,5
@@ -65,6 +78,11 @@ def test_stringify_path_localpath(self):
65
78
lpath = LocalPath (path )
66
79
assert common ._stringify_path (lpath ) == abs_path
67
80
81
+ def test_stringify_path_fspath (self ):
82
+ p = CustomFSPath ('foo/bar.csv' )
83
+ result = common ._stringify_path (p )
84
+ assert result == 'foo/bar.csv'
85
+
68
86
def test_get_filepath_or_buffer_with_path (self ):
69
87
filename = '~/sometest'
70
88
filepath_or_buffer , _ , _ = common .get_filepath_or_buffer (filename )
@@ -89,6 +107,69 @@ def test_iterator(self):
89
107
tm .assert_frame_equal (first , expected .iloc [[0 ]])
90
108
tm .assert_frame_equal (concat (it ), expected .iloc [1 :])
91
109
110
+ @pytest .mark .parametrize ('reader, module, path' , [
111
+ (pd .read_csv , 'os' , os .path .join (HERE , 'data' , 'iris.csv' )),
112
+ (pd .read_table , 'os' , os .path .join (HERE , 'data' , 'iris.csv' )),
113
+ (pd .read_fwf , 'os' , os .path .join (HERE , 'data' ,
114
+ 'fixed_width_format.txt' )),
115
+ (pd .read_excel , 'xlrd' , os .path .join (HERE , 'data' , 'test1.xlsx' )),
116
+ (pd .read_feather , 'feather' , os .path .join (HERE , 'data' ,
117
+ 'feather-0_3_1.feather' )),
118
+ (pd .read_hdf , 'tables' , os .path .join (HERE , 'data' , 'legacy_hdf' ,
119
+ 'datetimetz_object.h5' )),
120
+ (pd .read_stata , 'os' , os .path .join (HERE , 'data' , 'stata10_115.dta' )),
121
+ (pd .read_sas , 'os' , os .path .join (HERE , 'sas' , 'data' ,
122
+ 'test1.sas7bdat' )),
123
+ (pd .read_json , 'os' , os .path .join (HERE , 'json' , 'data' ,
124
+ 'tsframe_v012.json' )),
125
+ (pd .read_msgpack , 'os' , os .path .join (HERE , 'msgpack' , 'data' ,
126
+ 'frame.mp' )),
127
+ (pd .read_pickle , 'os' , os .path .join (HERE , 'data' ,
128
+ 'categorical_0_14_1.pickle' )),
129
+ ])
130
+ def test_read_fspath_all (self , reader , module , path ):
131
+ pytest .importorskip (module )
132
+ mypath = CustomFSPath (path )
133
+ result = reader (mypath )
134
+ expected = reader (mypath )
135
+ if path .endswith ('.pickle' ):
136
+ # categorical
137
+ tm .assert_categorical_equal (result , expected )
138
+ else :
139
+ tm .assert_frame_equal (result , expected )
140
+
141
+ @pytest .mark .parametrize ('writer_name, writer_kwargs, module' , [
142
+ ('to_csv' , {}, 'os' ),
143
+ ('to_excel' , {'engine' : 'xlwt' }, 'xlwt' ),
144
+ ('to_feather' , {}, 'feather' ),
145
+ ('to_hdf' , {'key' : 'bar' , 'mode' : 'w' }, 'tables' ),
146
+ ('to_html' , {}, 'os' ),
147
+ ('to_json' , {}, 'os' ),
148
+ ('to_latex' , {}, 'os' ),
149
+ ('to_msgpack' , {}, 'os' ),
150
+ ('to_pickle' , {}, 'os' ),
151
+ ('to_stata' , {}, 'os' ),
152
+ ])
153
+ def test_write_fspath_all (self , writer_name , writer_kwargs , module ):
154
+ p1 = tm .ensure_clean ('string' )
155
+ p2 = tm .ensure_clean ('fspath' )
156
+ df = pd .DataFrame ({"A" : [1 , 2 ]})
157
+
158
+ with p1 as string , p2 as fspath :
159
+ pytest .importorskip (module )
160
+ mypath = CustomFSPath (fspath )
161
+ writer = getattr (df , writer_name )
162
+
163
+ writer (string , ** writer_kwargs )
164
+ with open (string , 'rb' ) as f :
165
+ expected = f .read ()
166
+
167
+ writer (mypath , ** writer_kwargs )
168
+ with open (fspath , 'rb' ) as f :
169
+ result = f .read ()
170
+
171
+ assert result == expected
172
+
92
173
93
174
class TestMMapWrapper (object ):
94
175
0 commit comments