BUG: pd.concat with all Series on axis=1 ignores the `names` argument. #23490
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'])
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
On line 503 of pandas/core/reshape/concat.py the following line:
This solution solves provides the desired output for both examples above and does not break any of the test_concat.py tests.