Skip to content

Commit 1b576ca

Browse files
committed
BUG: Respect usecols even with empty data
Closes pandas-devgh-12493.
1 parent b874cb3 commit 1b576ca

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

pandas/io/parsers.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -1255,10 +1255,19 @@ def read(self, nrows=None):
12551255
except StopIteration:
12561256
if self._first_chunk:
12571257
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+
12621271
else:
12631272
raise
12641273

pandas/io/tests/test_parsers.py

+16
Original file line numberDiff line numberDiff line change
@@ -3807,6 +3807,22 @@ def test_buffer_rd_bytes(self):
38073807
except Exception as e:
38083808
pass
38093809

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+
38103826

38113827
class TestCParserHighMemory(CParserTests, CompressionTests, tm.TestCase):
38123828
engine = 'c'

0 commit comments

Comments
 (0)