|
14 | 14 | import pandas._tseries as lib
|
15 | 15 |
|
16 | 16 | class TestParsers(unittest.TestCase):
|
| 17 | + data1 = """index,A,B,C,D |
| 18 | +foo,2,3,4,5 |
| 19 | +bar,7,8,9,10 |
| 20 | +baz,12,13,14,15 |
| 21 | +qux,12,13,14,15 |
| 22 | +foo2,12,13,14,15 |
| 23 | +bar2,12,13,14,15 |
| 24 | +""" |
17 | 25 |
|
18 | 26 | def setUp(self):
|
19 | 27 | self.dirpath = curpath()
|
@@ -181,7 +189,9 @@ def test_read_table_duplicate_index(self):
|
181 | 189 | foo,12,13,14,15
|
182 | 190 | bar,12,13,14,15
|
183 | 191 | """
|
184 |
| - self.assertRaises(Exception, read_csv, StringIO(data), index_col=0) |
| 192 | + |
| 193 | + self.assertRaises(Exception, read_csv, StringIO(data), |
| 194 | + index_col=0) |
185 | 195 |
|
186 | 196 | def test_parse_bools(self):
|
187 | 197 | data = """A,B
|
@@ -211,6 +221,76 @@ def test_infer_index_col(self):
|
211 | 221 | data = read_csv(StringIO(data))
|
212 | 222 | self.assert_(data.index.equals(Index(['foo', 'bar', 'baz'])))
|
213 | 223 |
|
| 224 | + def test_read_nrows(self): |
| 225 | + df = read_csv(StringIO(self.data1), nrows=3) |
| 226 | + expected = read_csv(StringIO(self.data1))[:3] |
| 227 | + assert_frame_equal(df, expected) |
| 228 | + |
| 229 | + def test_read_chunksize(self): |
| 230 | + reader = read_csv(StringIO(self.data1), index_col=0, chunksize=2) |
| 231 | + df = read_csv(StringIO(self.data1), index_col=0) |
| 232 | + |
| 233 | + chunks = list(reader) |
| 234 | + |
| 235 | + assert_frame_equal(chunks[0], df[:2]) |
| 236 | + assert_frame_equal(chunks[1], df[2:4]) |
| 237 | + assert_frame_equal(chunks[2], df[4:]) |
| 238 | + |
| 239 | + def test_iterator(self): |
| 240 | + reader = read_csv(StringIO(self.data1), index_col=0, iterator=True) |
| 241 | + df = read_csv(StringIO(self.data1), index_col=0) |
| 242 | + |
| 243 | + chunk = reader.get_chunk(3) |
| 244 | + assert_frame_equal(chunk, df[:3]) |
| 245 | + |
| 246 | + last_chunk = reader.get_chunk(5) |
| 247 | + assert_frame_equal(last_chunk, df[3:]) |
| 248 | + |
| 249 | + def test_header_not_first_line(self): |
| 250 | + data = """got,to,ignore,this,line |
| 251 | +got,to,ignore,this,line |
| 252 | +index,A,B,C,D |
| 253 | +foo,2,3,4,5 |
| 254 | +bar,7,8,9,10 |
| 255 | +baz,12,13,14,15 |
| 256 | +""" |
| 257 | + data2 = """index,A,B,C,D |
| 258 | +foo,2,3,4,5 |
| 259 | +bar,7,8,9,10 |
| 260 | +baz,12,13,14,15 |
| 261 | +""" |
| 262 | + |
| 263 | + df = read_csv(StringIO(data), header=2, index_col=0) |
| 264 | + expected = read_csv(StringIO(data2), header=0, index_col=0) |
| 265 | + assert_frame_equal(df, expected) |
| 266 | + |
| 267 | + def test_pass_names_with_index(self): |
| 268 | + lines = self.data1.split('\n') |
| 269 | + no_header = '\n'.join(lines[1:]) |
| 270 | + |
| 271 | + # regular index |
| 272 | + names = ['index', 'A', 'B', 'C', 'D'] |
| 273 | + df = read_csv(StringIO(no_header), index_col=0, names=names) |
| 274 | + expected = read_csv(StringIO(self.data1), index_col=0) |
| 275 | + assert_frame_equal(df, expected) |
| 276 | + |
| 277 | + # multi index |
| 278 | + data = """index1,index2,A,B,C,D |
| 279 | +foo,one,2,3,4,5 |
| 280 | +foo,two,7,8,9,10 |
| 281 | +foo,three,12,13,14,15 |
| 282 | +bar,one,12,13,14,15 |
| 283 | +bar,two,12,13,14,15 |
| 284 | +""" |
| 285 | + lines = data.split('\n') |
| 286 | + no_header = '\n'.join(lines[1:]) |
| 287 | + names = ['index1', 'index2', 'A', 'B', 'C', 'D'] |
| 288 | + df = read_csv(StringIO(no_header), index_col=[0, 1], names=names) |
| 289 | + expected = read_csv(StringIO(data), index_col=[0, 1]) |
| 290 | + assert_frame_equal(df, expected) |
| 291 | + |
| 292 | + def test_multi_index_no_level_names(self): |
| 293 | + pass |
214 | 294 |
|
215 | 295 | class TestParseSQL(unittest.TestCase):
|
216 | 296 |
|
|
0 commit comments