Skip to content

BUG: pd.concat with all Series on axis=1 ignores the names argument. #23490

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
ms7463 opened this issue Nov 4, 2018 · 2 comments · Fixed by #23499
Closed

BUG: pd.concat with all Series on axis=1 ignores the names argument. #23490

ms7463 opened this issue Nov 4, 2018 · 2 comments · Fixed by #23499
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@ms7463
Copy link
Contributor

ms7463 commented Nov 4, 2018

Code Sample, a copy-pastable example if possible

# Example 1
s = pd.Series([1, 2, 3])
df = pd.concat([s, s], axis=1, keys=['a', 'b'], names=['Test'])
df.columns.names  # this gives back FrozenList([None]) instead of FrozenList(['Test'])
    
# Example 2
df = pd.concat([s, s], axis=1, keys=[('a', 1), ('b', 2)], names=['Test1', 'Test2'])
df.columns.names  # this gives back FrozenList([None, None]) instead of FrozenList(['Test1', 'Test2'])

Problem description

This is a problem because the API allows a user to provide a names argument with these input types and rather than throw an error or take the argument into consideration it is ignored. As far as I can tell, there is no reason for the names argument to be ignored, considering the keys are applied correctly, so I believe this was just an oversight in the core.reshape.concat._Concatenator._get_concat_axis method.

Proposed Solution

On line 503 of pandas/core/reshape/concat.py the following line:
return ensure_index(self.keys)
Should be replaced with something along these lines:

new_axis = ensure_index(self.keys)
new_axis.names = self.names
return new_axis

This solution solves provides the desired output for both examples above and does not break any of the test_concat.py tests.

Output of pd.show_versions()

pandas: 0.24.0.dev0

@ms7463 ms7463 changed the title BUG: pd.concat with all Serieson axis=1 ignores the names` argument. BUG: pd.concat with all Series on axis=1 ignores the names argument. Nov 4, 2018
@gfyoung gfyoung added Reshaping Concat, Merge/Join, Stack/Unstack, Explode Bug labels Nov 4, 2018
@gfyoung
Copy link
Member

gfyoung commented Nov 4, 2018

@ArtinSarraf : Good catch! Feel free to submit your patch as a PR!

@ms7463
Copy link
Contributor Author

ms7463 commented Nov 4, 2018

@gfyoung - Created the following PR #23499

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

Successfully merging a pull request may close this issue.

3 participants