@@ -608,10 +608,6 @@ def test_url(self):
608608
609609 @tm .slow
610610 def test_file (self ):
611-
612- # FILE
613- if sys .version_info [:2 ] < (2 , 6 ):
614- raise nose .SkipTest ("file:// not supported with Python < 2.6" )
615611 dirpath = tm .get_data_path ()
616612 localtable = os .path .join (dirpath , 'salary.table.csv' )
617613 local_table = self .read_table (localtable )
@@ -925,20 +921,22 @@ def test_empty_with_nrows_chunksize(self):
925921 StringIO ('foo,bar\n ' ), chunksize = 10 )))
926922 tm .assert_frame_equal (result , expected )
927923
928- # 'as_recarray' is not supported yet for the Python parser
929- if self . engine == 'c' :
924+ with tm . assert_produces_warning (
925+ FutureWarning , check_stacklevel = False ) :
930926 result = self .read_csv (StringIO ('foo,bar\n ' ),
931927 nrows = 10 , as_recarray = True )
932928 result = DataFrame (result [2 ], columns = result [1 ],
933929 index = result [0 ])
934930 tm .assert_frame_equal (DataFrame .from_records (
935931 result ), expected , check_index_type = False )
936932
937- result = next (iter (self .read_csv (
938- StringIO ('foo,bar\n ' ), chunksize = 10 , as_recarray = True )))
933+ with tm .assert_produces_warning (
934+ FutureWarning , check_stacklevel = False ):
935+ result = next (iter (self .read_csv (StringIO ('foo,bar\n ' ),
936+ chunksize = 10 , as_recarray = True )))
939937 result = DataFrame (result [2 ], columns = result [1 ], index = result [0 ])
940- tm .assert_frame_equal (DataFrame .from_records (
941- result ), expected , check_index_type = False )
938+ tm .assert_frame_equal (DataFrame .from_records (result ), expected ,
939+ check_index_type = False )
942940
943941 def test_eof_states (self ):
944942 # see gh-10728, gh-10548
@@ -1373,3 +1371,90 @@ def test_compact_ints_use_unsigned(self):
13731371 out = self .read_csv (StringIO (data ), compact_ints = True ,
13741372 use_unsigned = True )
13751373 tm .assert_frame_equal (out , expected )
1374+
1375+ def test_compact_ints_as_recarray (self ):
1376+ data = ('0,1,0,0\n '
1377+ '1,1,0,0\n '
1378+ '0,1,0,1' )
1379+
1380+ with tm .assert_produces_warning (
1381+ FutureWarning , check_stacklevel = False ):
1382+ result = self .read_csv (StringIO (data ), delimiter = ',' , header = None ,
1383+ compact_ints = True , as_recarray = True )
1384+ ex_dtype = np .dtype ([(str (i ), 'i1' ) for i in range (4 )])
1385+ self .assertEqual (result .dtype , ex_dtype )
1386+
1387+ with tm .assert_produces_warning (
1388+ FutureWarning , check_stacklevel = False ):
1389+ result = self .read_csv (StringIO (data ), delimiter = ',' , header = None ,
1390+ as_recarray = True , compact_ints = True ,
1391+ use_unsigned = True )
1392+ ex_dtype = np .dtype ([(str (i ), 'u1' ) for i in range (4 )])
1393+ self .assertEqual (result .dtype , ex_dtype )
1394+
1395+ def test_as_recarray (self ):
1396+ # basic test
1397+ with tm .assert_produces_warning (
1398+ FutureWarning , check_stacklevel = False ):
1399+ data = 'a,b\n 1,a\n 2,b'
1400+ expected = np .array ([(1 , 'a' ), (2 , 'b' )],
1401+ dtype = [('a' , '<i8' ), ('b' , 'O' )])
1402+ out = self .read_csv (StringIO (data ), as_recarray = True )
1403+ tm .assert_numpy_array_equal (out , expected )
1404+
1405+ # index_col ignored
1406+ with tm .assert_produces_warning (
1407+ FutureWarning , check_stacklevel = False ):
1408+ data = 'a,b\n 1,a\n 2,b'
1409+ expected = np .array ([(1 , 'a' ), (2 , 'b' )],
1410+ dtype = [('a' , '<i8' ), ('b' , 'O' )])
1411+ out = self .read_csv (StringIO (data ), as_recarray = True , index_col = 0 )
1412+ tm .assert_numpy_array_equal (out , expected )
1413+
1414+ # respects names
1415+ with tm .assert_produces_warning (
1416+ FutureWarning , check_stacklevel = False ):
1417+ data = '1,a\n 2,b'
1418+ expected = np .array ([(1 , 'a' ), (2 , 'b' )],
1419+ dtype = [('a' , '<i8' ), ('b' , 'O' )])
1420+ out = self .read_csv (StringIO (data ), names = ['a' , 'b' ],
1421+ header = None , as_recarray = True )
1422+ tm .assert_numpy_array_equal (out , expected )
1423+
1424+ # header order is respected even though it conflicts
1425+ # with the natural ordering of the column names
1426+ with tm .assert_produces_warning (
1427+ FutureWarning , check_stacklevel = False ):
1428+ data = 'b,a\n 1,a\n 2,b'
1429+ expected = np .array ([(1 , 'a' ), (2 , 'b' )],
1430+ dtype = [('b' , '<i8' ), ('a' , 'O' )])
1431+ out = self .read_csv (StringIO (data ), as_recarray = True )
1432+ tm .assert_numpy_array_equal (out , expected )
1433+
1434+ # overrides the squeeze parameter
1435+ with tm .assert_produces_warning (
1436+ FutureWarning , check_stacklevel = False ):
1437+ data = 'a\n 1'
1438+ expected = np .array ([(1 ,)], dtype = [('a' , '<i8' )])
1439+ out = self .read_csv (StringIO (data ), as_recarray = True , squeeze = True )
1440+ tm .assert_numpy_array_equal (out , expected )
1441+
1442+ # does data conversions before doing recarray conversion
1443+ with tm .assert_produces_warning (
1444+ FutureWarning , check_stacklevel = False ):
1445+ data = 'a,b\n 1,a\n 2,b'
1446+ conv = lambda x : int (x ) + 1
1447+ expected = np .array ([(2 , 'a' ), (3 , 'b' )],
1448+ dtype = [('a' , '<i8' ), ('b' , 'O' )])
1449+ out = self .read_csv (StringIO (data ), as_recarray = True ,
1450+ converters = {'a' : conv })
1451+ tm .assert_numpy_array_equal (out , expected )
1452+
1453+ # filters by usecols before doing recarray conversion
1454+ with tm .assert_produces_warning (
1455+ FutureWarning , check_stacklevel = False ):
1456+ data = 'a,b\n 1,a\n 2,b'
1457+ expected = np .array ([(1 ,), (2 ,)], dtype = [('a' , '<i8' )])
1458+ out = self .read_csv (StringIO (data ), as_recarray = True ,
1459+ usecols = ['a' ])
1460+ tm .assert_numpy_array_equal (out , expected )
0 commit comments