Skip to content

Commit 3c35897

Browse files
committed
Merge pull request #10302 from evanpw/empty_index_name
BUG: read_csv does not set index name on an empty DataFrame
2 parents 03ccc34 + 53f2170 commit 3c35897

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

doc/source/whatsnew/v0.16.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ Bug Fixes
150150
- Bug in `plot` not defaulting to matplotlib `axes.grid` setting (:issue:`9792`)
151151

152152
- Bug in ``Series.align`` resets ``name`` when ``fill_value`` is specified (:issue:`10067`)
153+
- Bug in ``read_csv`` causing index name not to be set on an empty DataFrame (:issue:`10184`)
153154
- Bug in ``SparseSeries.abs`` resets ``name`` (:issue:`10241`)
154155
- Bug in ``TimedeltaIndex`` slicing may reset freq (:issue:`10292`)
155156

pandas/io/parsers.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,9 @@ def read(self, nrows=None):
11701170
data = self._reader.read(nrows)
11711171
except StopIteration:
11721172
if nrows is None:
1173-
return None, self.names, {}
1173+
return _get_empty_meta(self.orig_names,
1174+
self.index_col,
1175+
self.index_names)
11741176
else:
11751177
raise
11761178

pandas/io/tests/test_parsers.py

+7
Original file line numberDiff line numberDiff line change
@@ -2294,6 +2294,13 @@ def test_chunk_begins_with_newline_whitespace(self):
22942294
result = self.read_csv(StringIO(data), header=None)
22952295
self.assertEqual(len(result), 2)
22962296

2297+
def test_empty_with_index(self):
2298+
# GH 10184
2299+
data = 'x,y'
2300+
result = self.read_csv(StringIO(data), index_col=0)
2301+
expected = DataFrame([], columns=['y'], index=Index([], name='x'))
2302+
tm.assert_frame_equal(result, expected)
2303+
22972304

22982305
class TestPythonParser(ParserTests, tm.TestCase):
22992306
def test_negative_skipfooter_raises(self):

0 commit comments

Comments
 (0)