Skip to content

DOC: pd.concat description of ordering when passing a mapping is unclear #58516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
wence- opened this issue May 1, 2024 · 2 comments · Fixed by #58518
Closed
1 task done

DOC: pd.concat description of ordering when passing a mapping is unclear #58516

wence- opened this issue May 1, 2024 · 2 comments · Fixed by #58518
Labels
Docs Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@wence-
Copy link
Contributor

wence- commented May 1, 2024

Pandas version checks

  • I have checked that the issue still exists on the latest versions of the docs on main here

Location of the documentation

https://pandas.pydata.org/docs/dev/reference/api/pandas.concat.html

Documentation problem

The objs parameter is described as:

objs: an iterable or mapping of Series or DataFrame objects

    If a mapping is passed, the sorted keys will be used as the keys argument, 
    unless it is passed, in which case the values will be selected (see below). 
    Any None objects will be dropped silently unless they are all None in 
    which case a ValueError will be raised.

My reading of this is that if I pass a dictionary of objects, then the order of the result of the concatenation will be given by sorted(objs.keys()) (if I don't pass keys=). However, it appears that the order is just objs.keys().

Example:

import pandas as pd
dfa = pd.DataFrame({"A": ["A"]})
dfb = pd.DataFrame({"B": ["B"]})

objs = {"Z": dfa, "Y": dfb}

# Expect these two to be the same
pd.concat(objs, axis=1)
#    Z  Y
#    A  B
# 0  A  B

pd.concat(objs, axis=1, keys=sorted(objs.keys())
#    Y  Z
#    B  A
# 0  B  A

Suggested fix for documentation

I think removed the statement of sortedness, not least because there is no requirement on the mapping input to have keys that admit a total order.

@wence- wence- added Docs Needs Triage Issue that has not been reviewed by a pandas team member labels May 1, 2024
@rhshadrach rhshadrach added Reshaping Concat, Merge/Join, Stack/Unstack, Explode and removed Needs Triage Issue that has not been reviewed by a pandas team member labels May 1, 2024
@rhshadrach
Copy link
Member

Thanks for the report! PR to fix is welcome!

@er-eis
Copy link
Contributor

er-eis commented May 1, 2024

i'll fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants