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
Comment From: jreback
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.
Comment From: jorisvandenbossche
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?