13
13
14
14
from pandas import DataFrame , Series , Index , MultiIndex , DatetimeIndex
15
15
from pandas .compat import (
16
- StringIO , BytesIO , PY3 , range , long , lrange , lmap , u , map , StringIO
16
+ StringIO , BytesIO , PY3 , range , long , lrange , lmap , u
17
17
)
18
18
from pandas .io .common import urlopen , URLError
19
19
import pandas .io .parsers as parsers
20
20
from pandas .io .parsers import (read_csv , read_table , read_fwf ,
21
21
TextFileReader , TextParser )
22
- from pandas .util .testing import (assert_equal ,
23
- assert_almost_equal ,
24
- assert_series_equal ,
25
- makeCustomDataframe as mkdf ,
26
- network ,
27
- ensure_clean )
22
+
28
23
import pandas .util .testing as tm
29
24
import pandas as pd
30
25
@@ -217,9 +212,9 @@ def test_1000_sep_with_decimal(self):
217
212
'C' : [5 , 10. ]
218
213
})
219
214
220
- assert_equal (expected .A .dtype , 'int64' )
221
- assert_equal (expected .B .dtype , 'float' )
222
- assert_equal (expected .C .dtype , 'float' )
215
+ tm . assert_equal (expected .A .dtype , 'int64' )
216
+ tm . assert_equal (expected .B .dtype , 'float' )
217
+ tm . assert_equal (expected .C .dtype , 'float' )
223
218
224
219
df = self .read_csv (StringIO (data ), sep = '|' , thousands = ',' , decimal = '.' )
225
220
tm .assert_frame_equal (df , expected )
@@ -247,7 +242,7 @@ def test_squeeze(self):
247
242
result = self .read_table (StringIO (data ), sep = ',' , index_col = 0 ,
248
243
header = None , squeeze = True )
249
244
tm .assert_isinstance (result , Series )
250
- assert_series_equal (result , expected )
245
+ tm . assert_series_equal (result , expected )
251
246
252
247
def test_inf_parsing (self ):
253
248
data = """\
@@ -265,9 +260,9 @@ def test_inf_parsing(self):
265
260
inf = float ('inf' )
266
261
expected = Series ([inf , - inf ] * 5 )
267
262
df = read_csv (StringIO (data ), index_col = 0 )
268
- assert_almost_equal (df ['A' ].values , expected .values )
263
+ tm . assert_almost_equal (df ['A' ].values , expected .values )
269
264
df = read_csv (StringIO (data ), index_col = 0 , na_filter = False )
270
- assert_almost_equal (df ['A' ].values , expected .values )
265
+ tm . assert_almost_equal (df ['A' ].values , expected .values )
271
266
272
267
def test_multiple_date_col (self ):
273
268
# Can use multiple date parsers
@@ -588,7 +583,7 @@ def test_passing_dtype(self):
588
583
589
584
df = DataFrame (np .random .rand (5 ,2 ),columns = list ('AB' ),index = ['1A' ,'1B' ,'1C' ,'1D' ,'1E' ])
590
585
591
- with ensure_clean ('__passing_str_as_dtype__.csv' ) as path :
586
+ with tm . ensure_clean ('__passing_str_as_dtype__.csv' ) as path :
592
587
df .to_csv (path )
593
588
594
589
# GH 3795
@@ -630,7 +625,7 @@ def test_quoting(self):
630
625
631
626
def test_non_string_na_values (self ):
632
627
# GH3611, na_values that are not a string are an issue
633
- with ensure_clean ('__non_string_na_values__.csv' ) as path :
628
+ with tm . ensure_clean ('__non_string_na_values__.csv' ) as path :
634
629
df = DataFrame ({'A' : [- 999 , 2 , 3 ], 'B' : [1.2 , - 999 , 4.5 ]})
635
630
df .to_csv (path , sep = ' ' , index = False )
636
631
result1 = read_csv (path , sep = ' ' , header = 0 , na_values = ['-999.0' ,'-999' ])
@@ -681,15 +676,15 @@ def test_custom_na_values(self):
681
676
[7 , 8 , nan ]]
682
677
683
678
df = self .read_csv (StringIO (data ), na_values = ['baz' ], skiprows = [1 ])
684
- assert_almost_equal (df .values , expected )
679
+ tm . assert_almost_equal (df .values , expected )
685
680
686
681
df2 = self .read_table (StringIO (data ), sep = ',' , na_values = ['baz' ],
687
682
skiprows = [1 ])
688
- assert_almost_equal (df2 .values , expected )
683
+ tm . assert_almost_equal (df2 .values , expected )
689
684
690
685
df3 = self .read_table (StringIO (data ), sep = ',' , na_values = 'baz' ,
691
686
skiprows = [1 ])
692
- assert_almost_equal (df3 .values , expected )
687
+ tm . assert_almost_equal (df3 .values , expected )
693
688
694
689
def test_nat_parse (self ):
695
690
@@ -699,7 +694,7 @@ def test_nat_parse(self):
699
694
'B' : pd .Timestamp ('20010101' ) }))
700
695
df .iloc [3 :6 ,:] = np .nan
701
696
702
- with ensure_clean ('__nat_parse_.csv' ) as path :
697
+ with tm . ensure_clean ('__nat_parse_.csv' ) as path :
703
698
df .to_csv (path )
704
699
result = read_csv (path ,index_col = 0 ,parse_dates = ['B' ])
705
700
tm .assert_frame_equal (result ,df )
@@ -750,7 +745,7 @@ def test_detect_string_na(self):
750
745
[nan , nan ]]
751
746
752
747
df = self .read_csv (StringIO (data ))
753
- assert_almost_equal (df .values , expected )
748
+ tm . assert_almost_equal (df .values , expected )
754
749
755
750
def test_unnamed_columns (self ):
756
751
data = """A,B,C,,
@@ -762,7 +757,7 @@ def test_unnamed_columns(self):
762
757
[6 , 7 , 8 , 9 , 10 ],
763
758
[11 , 12 , 13 , 14 , 15 ]]
764
759
df = self .read_table (StringIO (data ), sep = ',' )
765
- assert_almost_equal (df .values , expected )
760
+ tm . assert_almost_equal (df .values , expected )
766
761
self .assert_ (np .array_equal (df .columns ,
767
762
['A' , 'B' , 'C' , 'Unnamed: 3' ,
768
763
'Unnamed: 4' ]))
@@ -913,8 +908,8 @@ def test_no_header(self):
913
908
expected = [[1 , 2 , 3 , 4 , 5. ],
914
909
[6 , 7 , 8 , 9 , 10 ],
915
910
[11 , 12 , 13 , 14 , 15 ]]
916
- assert_almost_equal (df .values , expected )
917
- assert_almost_equal (df .values , df2 .values )
911
+ tm . assert_almost_equal (df .values , expected )
912
+ tm . assert_almost_equal (df .values , df2 .values )
918
913
919
914
self .assert_ (np .array_equal (df_pref .columns ,
920
915
['X0' , 'X1' , 'X2' , 'X3' , 'X4' ]))
@@ -1177,7 +1172,7 @@ def test_header_not_first_line(self):
1177
1172
tm .assert_frame_equal (df , expected )
1178
1173
1179
1174
def test_header_multi_index (self ):
1180
- expected = mkdf (5 ,3 ,r_idx_nlevels = 2 ,c_idx_nlevels = 4 )
1175
+ expected = tm . makeCustomDataframe (5 ,3 ,r_idx_nlevels = 2 ,c_idx_nlevels = 4 )
1181
1176
1182
1177
data = """\
1183
1178
C0,,C_l0_g0,C_l0_g1,C_l0_g2
@@ -1477,7 +1472,7 @@ def test_na_value_dict(self):
1477
1472
tm .assert_frame_equal (df , xp )
1478
1473
1479
1474
@slow
1480
- @network
1475
+ @tm . network
1481
1476
def test_url (self ):
1482
1477
try :
1483
1478
# HTTP(S)
@@ -1492,7 +1487,7 @@ def test_url(self):
1492
1487
1493
1488
except URLError :
1494
1489
try :
1495
- with closing (urlopen ('http://www.google.com' )) as resp :
1490
+ with tm . closing (urlopen ('http://www.google.com' )) as resp :
1496
1491
pass
1497
1492
except URLError :
1498
1493
raise nose .SkipTest
@@ -1597,11 +1592,11 @@ def test_comment(self):
1597
1592
expected = [[1. , 2. , 4. ],
1598
1593
[5. , np .nan , 10. ]]
1599
1594
df = self .read_csv (StringIO (data ), comment = '#' )
1600
- assert_almost_equal (df .values , expected )
1595
+ tm . assert_almost_equal (df .values , expected )
1601
1596
1602
1597
df = self .read_table (StringIO (data ), sep = ',' , comment = '#' ,
1603
1598
na_values = ['NaN' ])
1604
- assert_almost_equal (df .values , expected )
1599
+ tm . assert_almost_equal (df .values , expected )
1605
1600
1606
1601
def test_bool_na_values (self ):
1607
1602
data = """A,B,C
@@ -1659,7 +1654,7 @@ def test_utf16_bom_skiprows(self):
1659
1654
1660
1655
path = '__%s__.csv' % tm .rands (10 )
1661
1656
1662
- with ensure_clean (path ) as path :
1657
+ with tm . ensure_clean (path ) as path :
1663
1658
for sep , dat in [('\t ' , data ), (',' , data2 )]:
1664
1659
for enc in ['utf-16' , 'utf-16le' , 'utf-16be' ]:
1665
1660
bytes = dat .encode (enc )
@@ -1924,7 +1919,7 @@ def test_1000_fwf(self):
1924
1919
[10 , 13 , 10 ]]
1925
1920
df = read_fwf (StringIO (data ), colspecs = [(0 , 3 ), (3 , 11 ), (12 , 16 )],
1926
1921
thousands = ',' )
1927
- assert_almost_equal (df .values , expected )
1922
+ tm . assert_almost_equal (df .values , expected )
1928
1923
1929
1924
def test_1000_sep_with_decimal (self ):
1930
1925
data = """A|B|C
@@ -1953,7 +1948,7 @@ def test_comment_fwf(self):
1953
1948
[5 , np .nan , 10. ]]
1954
1949
df = read_fwf (StringIO (data ), colspecs = [(0 , 3 ), (4 , 9 ), (9 , 25 )],
1955
1950
comment = '#' )
1956
- assert_almost_equal (df .values , expected )
1951
+ tm . assert_almost_equal (df .values , expected )
1957
1952
1958
1953
def test_fwf (self ):
1959
1954
data_expected = """\
@@ -2075,7 +2070,7 @@ def test_iteration_open_handle(self):
2075
2070
if PY3 :
2076
2071
raise nose .SkipTest
2077
2072
2078
- with ensure_clean () as path :
2073
+ with tm . ensure_clean () as path :
2079
2074
with open (path , 'wb' ) as f :
2080
2075
f .write ('AAA\n BBB\n CCC\n DDD\n EEE\n FFF\n GGG' )
2081
2076
@@ -2294,7 +2289,7 @@ def test_decompression(self):
2294
2289
data = open (self .csv1 , 'rb' ).read ()
2295
2290
expected = self .read_csv (self .csv1 )
2296
2291
2297
- with ensure_clean () as path :
2292
+ with tm . ensure_clean () as path :
2298
2293
tmp = gzip .GzipFile (path , mode = 'wb' )
2299
2294
tmp .write (data )
2300
2295
tmp .close ()
@@ -2305,7 +2300,7 @@ def test_decompression(self):
2305
2300
result = self .read_csv (open (path , 'rb' ), compression = 'gzip' )
2306
2301
tm .assert_frame_equal (result , expected )
2307
2302
2308
- with ensure_clean () as path :
2303
+ with tm . ensure_clean () as path :
2309
2304
tmp = bz2 .BZ2File (path , mode = 'wb' )
2310
2305
tmp .write (data )
2311
2306
tmp .close ()
@@ -2330,15 +2325,15 @@ def test_decompression_regex_sep(self):
2330
2325
data = data .replace (b',' , b'::' )
2331
2326
expected = self .read_csv (self .csv1 )
2332
2327
2333
- with ensure_clean () as path :
2328
+ with tm . ensure_clean () as path :
2334
2329
tmp = gzip .GzipFile (path , mode = 'wb' )
2335
2330
tmp .write (data )
2336
2331
tmp .close ()
2337
2332
2338
2333
result = self .read_csv (path , sep = '::' , compression = 'gzip' )
2339
2334
tm .assert_frame_equal (result , expected )
2340
2335
2341
- with ensure_clean () as path :
2336
+ with tm . ensure_clean () as path :
2342
2337
tmp = bz2 .BZ2File (path , mode = 'wb' )
2343
2338
tmp .write (data )
2344
2339
tmp .close ()
@@ -2552,7 +2547,7 @@ def test_convert_sql_column_decimals(self):
2552
2547
2553
2548
def assert_same_values_and_dtype (res , exp ):
2554
2549
assert (res .dtype == exp .dtype )
2555
- assert_almost_equal (res , exp )
2550
+ tm . assert_almost_equal (res , exp )
2556
2551
2557
2552
2558
2553
if __name__ == '__main__' :
0 commit comments