Skip to content

Commit 5d6cc60

Browse files
cmazzulloTomAugspurger
authored andcommitted
DOC: header='infer' is not working when there is no header, closes #17473 (#18042)
(cherry picked from commit 67c4d0f)
1 parent bfeda79 commit 5d6cc60

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

doc/source/io.rst

+22-9
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,20 @@ Column and Index Locations and Names
103103
++++++++++++++++++++++++++++++++++++
104104

105105
header : int or list of ints, default ``'infer'``
106-
Row number(s) to use as the column names, and the start of the data. Default
107-
behavior is as if ``header=0`` if no ``names`` passed, otherwise as if
108-
``header=None``. Explicitly pass ``header=0`` to be able to replace existing
109-
names. The header can be a list of ints that specify row locations for a
110-
multi-index on the columns e.g. ``[0,1,3]``. Intervening rows that are not
111-
specified will be skipped (e.g. 2 in this example is skipped). Note that
112-
this parameter ignores commented lines and empty lines if
113-
``skip_blank_lines=True``, so header=0 denotes the first line of data
114-
rather than the first line of the file.
106+
Row number(s) to use as the column names, and the start of the
107+
data. Default behavior is to infer the column names: if no names are
108+
passed the behavior is identical to ``header=0`` and column names
109+
are inferred from the first line of the file, if column names are
110+
passed explicitly then the behavior is identical to
111+
``header=None``. Explicitly pass ``header=0`` to be able to replace
112+
existing names.
113+
114+
The header can be a list of ints that specify row locations
115+
for a multi-index on the columns e.g. ``[0,1,3]``. Intervening rows
116+
that are not specified will be skipped (e.g. 2 in this example is
117+
skipped). Note that this parameter ignores commented lines and empty
118+
lines if ``skip_blank_lines=True``, so header=0 denotes the first
119+
line of data rather than the first line of the file.
115120
names : array-like, default ``None``
116121
List of column names to use. If file contains no header row, then you should
117122
explicitly pass ``header=None``. Duplicates in this list will cause
@@ -553,6 +558,14 @@ If the header is in a row other than the first, pass the row number to
553558
data = 'skip this skip it\na,b,c\n1,2,3\n4,5,6\n7,8,9'
554559
pd.read_csv(StringIO(data), header=1)
555560
561+
.. note::
562+
563+
Default behavior is to infer the column names: if no names are
564+
passed the behavior is identical to ``header=0`` and column names
565+
are inferred from the first nonblank line of the file, if column
566+
names are passed explicitly then the behavior is identical to
567+
``header=None``.
568+
556569
.. _io.dupe_names:
557570

558571
Duplicate names parsing

pandas/io/parsers.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,19 @@
7474
.. versionadded:: 0.18.1 support for the Python parser.
7575
7676
header : int or list of ints, default 'infer'
77-
Row number(s) to use as the column names, and the start of the data.
78-
Default behavior is as if set to 0 if no ``names`` passed, otherwise
79-
``None``. Explicitly pass ``header=0`` to be able to replace existing
80-
names. The header can be a list of integers that specify row locations for
81-
a multi-index on the columns e.g. [0,1,3]. Intervening rows that are not
82-
specified will be skipped (e.g. 2 in this example is skipped). Note that
83-
this parameter ignores commented lines and empty lines if
84-
``skip_blank_lines=True``, so header=0 denotes the first line of data
85-
rather than the first line of the file.
77+
Row number(s) to use as the column names, and the start of the
78+
data. Default behavior is to infer the column names: if no names
79+
are passed the behavior is identical to ``header=0`` and column
80+
names are inferred from the first line of the file, if column
81+
names are passed explicitly then the behavior is identical to
82+
``header=None``. Explicitly pass ``header=0`` to be able to
83+
replace existing names. The header can be a list of integers that
84+
specify row locations for a multi-index on the columns
85+
e.g. [0,1,3]. Intervening rows that are not specified will be
86+
skipped (e.g. 2 in this example is skipped). Note that this
87+
parameter ignores commented lines and empty lines if
88+
``skip_blank_lines=True``, so header=0 denotes the first line of
89+
data rather than the first line of the file.
8690
names : array-like, default None
8791
List of column names to use. If file contains no header row, then you
8892
should explicitly pass header=None. Duplicates in this list will cause

0 commit comments

Comments
 (0)