File tree 2 files changed +29
-4
lines changed
2 files changed +29
-4
lines changed Original file line number Diff line number Diff line change @@ -1255,10 +1255,19 @@ def read(self, nrows=None):
1255
1255
except StopIteration :
1256
1256
if self ._first_chunk :
1257
1257
self ._first_chunk = False
1258
- return _get_empty_meta (self .orig_names ,
1259
- self .index_col ,
1260
- self .index_names ,
1261
- dtype = self .kwds .get ('dtype' ))
1258
+
1259
+ index , columns , col_dict = _get_empty_meta (
1260
+ self .orig_names , self .index_col ,
1261
+ self .index_names , dtype = self .kwds .get ('dtype' ))
1262
+
1263
+ if self .usecols is not None :
1264
+ columns = self ._filter_usecols (columns )
1265
+
1266
+ col_dict = dict (filter (lambda item : item [0 ] in columns ,
1267
+ col_dict .items ()))
1268
+
1269
+ return index , columns , col_dict
1270
+
1262
1271
else :
1263
1272
raise
1264
1273
Original file line number Diff line number Diff line change @@ -3807,6 +3807,22 @@ def test_buffer_rd_bytes(self):
3807
3807
except Exception as e :
3808
3808
pass
3809
3809
3810
+ def test_read_empty_with_usecols (self ):
3811
+ # See gh-12493
3812
+ names = ['Dummy' , 'X' , 'Dummy_2' ]
3813
+ usecols = names [1 :2 ] # ['X']
3814
+
3815
+ def check_usecols (stringIO , expected ):
3816
+ df = read_csv (stringIO , names = names , usecols = usecols )
3817
+ tm .assert_frame_equal (df , expected )
3818
+
3819
+ expected = DataFrame (columns = usecols ,
3820
+ index = [0 ], dtype = np .float64 )
3821
+ check_usecols (StringIO (',,' ), expected )
3822
+
3823
+ expected = DataFrame (columns = usecols )
3824
+ check_usecols (StringIO ('' ), expected )
3825
+
3810
3826
3811
3827
class TestCParserHighMemory (CParserTests , CompressionTests , tm .TestCase ):
3812
3828
engine = 'c'
You can’t perform that action at this time.
0 commit comments