pandas-dev / pandas-dev/pandas

DOC:/USAGE: Resulting Index when merging over indices

Open
#34,412 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Docs MultiIndex Reshaping
Dominant language
Python
Stars
49.8k
Forks
20.4k
Avg merge
1d 16h
Merged PRs (30d)
322

Description

Location of the documentation

https://dev.pandas.io/docs/reference/api/pandas.DataFrame.merge.html

Documentation problem

There is no documentation on this case

Suggested fix for documentation

I'm a bit confused about the behavior here, because I don't know what to expect. When I perform the merge as shown below, I would expect that both operations return the same output.

left = pd.DataFrame({'a': [1, 2], 'b': [1, 1], "l": [22, 23]}).set_index(['a', 'b'])
right = pd.DataFrame({'b': [1], "r": [12]}).set_index(['b'])
print(pd.merge(left, right, left_on=['b'], right_index=True, how="left"))
print(pd.merge(left, right, left_on=['b'], right_on=["b"], how="left"))

But the Index of both results differs. The first merge has the same index as the left DataFrame while the second merge has only b as index. As a result, the index from the second merge is no longer unique.

First Merge:

      l   r
a b        
1 1  22  12
2 1  23  12

Second Merge:

    l   r
b        
1  22  12
1  23  12

If this is the desired behavior, we should adjust the documentation to show this. If it is not, I would file an BUG issue. We should adjust the documentation nevertheless. I would add an example with an index join to show the right behavior.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the pandas.DataFrame.merge documentation URL and reproduce the two index-merge examples from the issue. Compare the documented behavior with both outputs and determine whether the index difference is intended. Done means the expected behavior is confirmed and the merge documentation includes a clear example covering this case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data, documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.