From 53f21708c99748c9f25b1371c26d4da7168e9145 Mon Sep 17 00:00:00 2001 From: Evan Wright Date: Sat, 6 Jun 2015 12:55:24 -0400 Subject: [PATCH] BUG: read_csv does not set index name on an empty DataFrame (GH #10184) --- doc/source/whatsnew/v0.16.2.txt | 1 + pandas/io/parsers.py | 4 +++- pandas/io/tests/test_parsers.py | 7 +++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.16.2.txt b/doc/source/whatsnew/v0.16.2.txt index 9421ab0f841ac..399402d243be7 100644 --- a/doc/source/whatsnew/v0.16.2.txt +++ b/doc/source/whatsnew/v0.16.2.txt @@ -142,6 +142,7 @@ Bug Fixes - Bug in `plot` not defaulting to matplotlib `axes.grid` setting (:issue:`9792`) - Bug in ``Series.align`` resets ``name`` when ``fill_value`` is specified (:issue:`10067`) +- Bug in ``read_csv`` causing index name not to be set on an empty DataFrame (:issue:`10184`) - Bug in ``SparseSeries.abs`` resets ``name`` (:issue:`10241`) diff --git a/pandas/io/parsers.py b/pandas/io/parsers.py index 59ecb29146315..ce8ac1c93e596 100755 --- a/pandas/io/parsers.py +++ b/pandas/io/parsers.py @@ -1170,7 +1170,9 @@ def read(self, nrows=None): data = self._reader.read(nrows) except StopIteration: if nrows is None: - return None, self.names, {} + return _get_empty_meta(self.orig_names, + self.index_col, + self.index_names) else: raise diff --git a/pandas/io/tests/test_parsers.py b/pandas/io/tests/test_parsers.py index 1177149e7efa6..a8a5de38f257c 100755 --- a/pandas/io/tests/test_parsers.py +++ b/pandas/io/tests/test_parsers.py @@ -2294,6 +2294,13 @@ def test_chunk_begins_with_newline_whitespace(self): result = self.read_csv(StringIO(data), header=None) self.assertEqual(len(result), 2) + def test_empty_with_index(self): + # GH 10184 + data = 'x,y' + result = self.read_csv(StringIO(data), index_col=0) + expected = DataFrame([], columns=['y'], index=Index([], name='x')) + tm.assert_frame_equal(result, expected) + class TestPythonParser(ParserTests, tm.TestCase): def test_negative_skipfooter_raises(self):