Skip to content

DOC: clarify scalar vs list-like return types for .loc and []#65642

Merged
mroeschke merged 2 commits into
pandas-dev:mainfrom
jbrockmendel:doc-46593
May 16, 2026
Merged

DOC: clarify scalar vs list-like return types for .loc and []#65642
mroeschke merged 2 commits into
pandas-dev:mainfrom
jbrockmendel:doc-46593

Conversation

@jbrockmendel
Copy link
Copy Markdown
Member

closes #46593

Summary

  • Add two rows to the indexing basics table for frame[[colname]] and frame[list_of_colnames] (per @CloseChoice's suggestion in DOC: .loc improvement #46593).
  • Add a short note to the .loc docstring explaining that the form of the indexer (scalar vs. list-like) determines the return type, with the duplicate-labels exception spelled out.

Test plan

  • Doctests pass for pandas/core/indexing.py (9 passed)
  • pre-commit clean on changed files

closes pandas-dev#46593

Extend the indexing basics table with rows for the list-like
``DataFrame`` cases, and add a short note to the ``.loc`` docstring
explaining that the *form* of the indexer (scalar vs. list-like) is
what determines the return type, with the duplicate-labels exception
called out.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Comment thread pandas/core/indexing.py Outdated
- 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)

@mroeschke mroeschke added the Docs label May 15, 2026
Copy link
Copy Markdown
Member

@CloseChoice CloseChoice left a comment

Choose a reason for hiding this comment

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

Since I was mentioned in the PR description, I'll leave a review here. Take it for what you deem it's worth.

As mentioned by @mroeschke I would also appreciate an example + doctest. Otherwise this looks good and exactly what I had in mind when writing the referenced comment.

Per reviewer feedback (@mroeschke, @CloseChoice), swap the prose
paragraph for a doctest-checked example pair contrasting
``df.loc[:, "max_speed"]`` (Series) with ``df.loc[:, ["max_speed"]]``
(DataFrame), so the documented behavior can't drift from reality.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@mroeschke mroeschke added this to the 3.1 milestone May 16, 2026
@mroeschke mroeschke merged commit 47a404d into pandas-dev:main May 16, 2026
46 checks passed
@mroeschke
Copy link
Copy Markdown
Member

Thanks @jbrockmendel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DOC: .loc improvement

3 participants