Skip to content

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

Closed
@ms7463

Description

@ms7463

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions