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

Index Name vanishes when calling concat on two objects with different order indices #19616

Closed
swarnakumar opened this issue Feb 9, 2018 · 2 comments
Labels
Indexing Related to indexing on series/frames, not to indexes themselves Usage Question

Comments

@swarnakumar
Copy link

Using pd.concat results in index name disappearing when the index is not in the same order for DataFrames being concatenated

>>> df1 = pd.DataFrame({'a': [1, 2], 'b': [3,4]}, index=pd.Index(['a', 'b'], name='ind'))

>>> df1
     a  b
ind
a    1  3
b    2  4

Now create another dataframe, with the same index, but in a different order

>>> df2 = pd.DataFrame({'c': [4,3]}, index=pd.Index(['b', 'a'], name='ind'))

>>> df2
     c
ind
b    4
a    3

>>> df = pd.concat([df1, df2], axis=1)

>>> df.index.names
FrozenList([None])

Problem description

This is inconsistent with behaviour with the index is ordered the same for both DataFrames.

The right behaviour should be: the index name always being maintained

Expected Output

>>> df.index.names == df1.index.names == df2.index.names
True

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.2.final.0
python-bits: 64
OS: Darwin
OS-release: 17.4.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.21.1
pytest: 3.3.2
pip: 9.0.1
setuptools: 38.2.4
Cython: 0.27.3
numpy: 1.13.3
scipy: 1.0.0
pyarrow: 0.8.0
xarray: None
IPython: 6.2.1
sphinx: None
patsy: 0.5.0
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: None
tables: None
numexpr: 2.6.4
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@jreback
Copy link
Contributor

jreback commented Feb 9, 2018

index names is removed if the indexes are not equal. indexes are ordered, thus these are not equal and hence the name is set to None

you could reindex first if you want then they would be equal.

@jreback jreback closed this as completed Feb 9, 2018
@jreback jreback added Indexing Related to indexing on series/frames, not to indexes themselves Usage Question labels Feb 9, 2018
@jreback jreback added this to the No action milestone Feb 9, 2018
@jorisvandenbossche
Copy link
Member

Wouldn't it actually make more sense to look at the equality of the index name instead of the values to determine if the name is kept?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves Usage Question
Projects
None yet
Development

No branches or pull requests

3 participants