Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions doc/source/user_guide/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ indexing pandas objects with ``[]``:

Series, ``series[label]``, scalar value
DataFrame, ``frame[colname]``, ``Series`` corresponding to colname
DataFrame, ``frame[[colname]]``, ``DataFrame`` with columns corresponding to ``[colname]``
DataFrame, ``frame[list_of_colnames]``, ``DataFrame`` with columns corresponding to ``list_of_colnames``

The same principle applies to label-based selection with ``.loc``: a
list-like of labels preserves the corresponding axis (so
``df.loc[:, ["A"]]`` returns a ``DataFrame``, even when the list contains a
single label), while a scalar label reduces the axis when labels on that
axis are unique (so ``df.loc[:, "A"]`` typically returns a ``Series``; if
``"A"`` is duplicated on the axis, a ``DataFrame`` is returned instead).
See :ref:`indexing.label` for details.

Here we construct a simple time series data set to use for illustrating the
indexing functionality:
Expand Down
8 changes: 8 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,14 @@ class _LocIndexer(_LocationIndexer):
- A ``callable`` function with one argument (the calling Series or
DataFrame) and that returns valid output for indexing (one of the above)

The *form* of the indexer, not the number of items it selects,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we show this as an example instead (note: I'm generally more fond of using Examples w/ doctest to ensure what we're describing doesn't go stale)

determines the return type. A list-like of labels preserves the
corresponding axis, so ``df.loc[:, ["A"]]`` returns a ``DataFrame``
even when the list contains a single label. A scalar label reduces
the axis when labels on that axis are unique, so ``df.loc[:, "A"]``
typically returns a ``Series`` (if ``"A"`` is duplicated, the axis
is preserved and a ``DataFrame`` is returned instead).

See more at :ref:`Selection by Label <indexing.label>`.

Raises
Expand Down
Loading