Skip to content
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

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

ArtinSarraf opened this issue Nov 4, 2018 · 2 comments


None yet
3 participants
Copy link

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/ 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 tests.

Output of pd.show_versions()

pandas: 0.24.0.dev0

@ArtinSarraf ArtinSarraf changed the title BUG: pd.concat with all Series`on axis=1 ignores the `names` argument. BUG: pd.concat with all Series on axis=1 ignores the `names` argument. Nov 4, 2018


This comment has been minimized.

Copy link

commented Nov 4, 2018

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


This comment has been minimized.

Copy link
Contributor Author

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
You can’t perform that action at this time.