Skip to content

pandas.concat bug with series of the same name and axis=1 #2972

Closed
@lesteve

Description

@lesteve

Kind of an edge case I would say, but still it would be nice to get an error or have an automatic renaming of the columns. If a simple error is acceptable as a first step I could definitely put together a PR.

The following snippet shows that when you concatenate a column of zeros and a column of ones the resulting dataframe has two columns of ones:

import pandas as pd
import numpy as np
ts0 = pd.Series(np.zeros(5))
ts1 = pd.Series(np.ones(5))
ts0.name = ts1.name = 'same name'
df = pd.concat([ts0, ts1], axis=1)
print df

and the output (pandas 0.10.1):

   same name  same name
0          1          1
1          1          1
2          1          1
3          1          1
4          1          1

whereas I would expect:

   same name  same name
0          0          1
1          0          1
2          0          1
3          0          1
4          0          1

Looks like this is due to pandas.tools.merge._Concatenator._get_concat_axis which doesn't check for name uniqueness, here is the relevant snippet:

names = []
for x in self.objs:
    if x.name is not None:
        names.append(x.name)
    else:
        return Index(np.arange(len(self.objs))
return Index(names)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions