Skip to content

Commit ab6c241

Browse files
phoflmeeseeksmachine
authored andcommitted
Backport PR pandas-dev#47139: REGR: index_col False and header=None inferring index names in some cases
1 parent 3b3162b commit ab6c241

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

doc/source/whatsnew/v1.4.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Fixed regressions
1919
- Fixed regression in :func:`read_fwf` raising ``ValueError`` when ``widths`` was specified with ``usecols`` (:issue:`46580`)
2020
- Fixed regression in :meth:`.Groupby.transform` and :meth:`.Groupby.agg` failing with ``engine="numba"`` when the index was a :class:`MultiIndex` (:issue:`46867`)
2121
- Fixed regression is :meth:`.Styler.to_latex` and :meth:`.Styler.to_html` where ``buf`` failed in combination with ``encoding`` (:issue:`47053`)
22+
- Fixed regression in :func:`read_csv` with ``index_col=False`` identifying first row as index names when ``header=None`` (:issue:`46955`)
2223
- Fixed regression in :meth:`.DataFrameGroupBy.agg` when used with list-likes or dict-likes and ``axis=1`` that would give incorrect results; now raises ``NotImplementedError`` (:issue:`46995`)
2324
- Fixed regression in :meth:`DataFrame.resample` and :meth:`DataFrame.rolling` when used with list-likes or dict-likes and ``axis=1`` that would raise an unintuitive error message; now raises ``NotImplementedError`` (:issue:`46904`)
2425

pandas/io/parsers/python_parser.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ def _get_index_name(self, columns: list[Hashable]):
917917
implicit_first_cols = len(line) - self.num_original_columns
918918

919919
# Case 0
920-
if next_line is not None:
920+
if next_line is not None and self.header is not None:
921921
if len(next_line) == len(line) + self.num_original_columns:
922922
# column and index names on diff rows
923923
self.index_col = list(range(len(line)))

pandas/tests/io/parser/test_python_parser_only.py

+12
Original file line numberDiff line numberDiff line change
@@ -458,3 +458,15 @@ def test_on_bad_lines_index_col_inferred(python_parser_only):
458458
result = parser.read_csv(bad_sio, on_bad_lines=lambda x: ["99", "99"])
459459
expected = DataFrame({"a": [2, 5], "b": [3, 6]}, index=[1, 4])
460460
tm.assert_frame_equal(result, expected)
461+
462+
463+
def test_index_col_false_and_header_none(python_parser_only):
464+
# GH#46955
465+
parser = python_parser_only
466+
data = """
467+
0.5,0.03
468+
0.1,0.2,0.3,2
469+
"""
470+
result = parser.read_csv(StringIO(data), sep=",", header=None, index_col=False)
471+
expected = DataFrame({0: [0.5, 0.1], 1: [0.03, 0.2]})
472+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)