Skip to content

multi-index display bug #12223

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

Closed
s-wakaba opened this issue Feb 3, 2016 · 1 comment
Closed

multi-index display bug #12223

s-wakaba opened this issue Feb 3, 2016 · 1 comment

Comments

@s-wakaba
Copy link

s-wakaba commented Feb 3, 2016

Thanks for great tool!

When printing a long DataFrame with multi-index which are result of set_index(..., append=True),
displaying index values look strange.

import pandas as pd

def func(n):
    x = [
        [['a1-%d' % i, 'a2-%d' % i] for i in range(n)],
        [['b1-%d' % i, 'b2-%d' % i] for i in range(n)],
    ]
    y = pd.concat([pd.DataFrame(a) for a in x], keys=['X', 'Y']).set_index(1, append=True)
    print(y)

func(5) # no problem
func(50) # bug!

When DataFrames are short (in this case, 10 rows), there is no problem.
However, when DataFrames is long (in this case, 100 rows) and omitted middle-part on printing, some columns of index is not correct.

             0
    1         
X 0 a2-0  a1-0
  1 a2-1  a1-1
  2 a2-2  a1-2
  3 a2-3  a1-3
  4 a2-4  a1-4
Y 0 b2-0  b1-0
  1 b2-1  b1-1
  2 b2-2  b1-2
  3 b2-3  b1-3
  4 b2-4  b1-4
                   0
        1           
X a2-0  a2-0    a1-0
  a2-1  a2-1    a1-1
  a2-2  a2-2    a1-2
  a2-3  a2-3    a1-3
  a2-4  a2-4    a1-4
  a2-5  a2-5    a1-5
  a2-6  a2-6    a1-6
  a2-7  a2-7    a1-7
  a2-8  a2-8    a1-8
  a2-9  a2-9    a1-9
...              ...
Y b2-40 b2-40  b1-40
  b2-41 b2-41  b1-41
  b2-42 b2-42  b1-42
  b2-43 b2-43  b1-43
  b2-44 b2-44  b1-44
  b2-45 b2-45  b1-45
  b2-46 b2-46  b1-46
  b2-47 b2-47  b1-47
  b2-48 b2-48  b1-48
  b2-49 b2-49  b1-49

[100 rows x 1 columns]

result of pd.show_versions() is as follows.

INSTALLED VERSIONS
------------------
commit: None
python: 3.5.1.final.0
python-bits: 64
OS: Linux
OS-release: 2.6.32-279.22.1.el6.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.17.1
nose: 1.3.7
pip: 7.1.2
setuptools: 18.5
Cython: 0.23.4
numpy: 1.10.1
scipy: 0.16.0
statsmodels: 0.6.1
IPython: 4.0.1
sphinx: 1.3.1
patsy: 0.4.0
dateutil: 2.4.2
pytz: 2015.7
blosc: None
bottleneck: 1.0.0
tables: 3.2.2
numexpr: 2.4.4
matplotlib: 1.5.0
openpyxl: 2.2.6
xlrd: 0.9.4
xlwt: 1.0.0
xlsxwriter: 0.7.7
lxml: 3.4.4
bs4: 4.4.1
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.9
pymysql: None
psycopg2: None
Jinja2: None
@sinhrks
Copy link
Member

sinhrks commented Feb 6, 2016

Related to #10461.

This is caused by MultiIndex.append internally used. The 2nd level is overwritten by level named with "1".

import pandas as pd

# OK
idx1 = pd.MultiIndex.from_tuples([(1, 'A', 1), (1, 'A', 2)])
idx1.append(idx1)
# MultiIndex(levels=[[1], [u'A'], [1, 2]],
#            labels=[[0, 0, 0, 0], [0, 0, 0, 0], [0, 1, 0, 1]])

# NG, index name has number which is different from location
idx2 = idx1.set_names([0, 'a', 1])
idx2.append(idx2)
# MultiIndex(levels=[[1], [1, 2], [1, 2]],
#            labels=[[0, 0, 0, 0], [0, 1, 0, 1], [0, 1, 0, 1]],
#            names=[0, u'a', 1])

As a workaround, do not use numeric names.

Dr-Irv added a commit to Dr-Irv/pandas that referenced this issue Feb 22, 2017
Dr-Irv added a commit to Dr-Irv/pandas that referenced this issue Feb 23, 2017
@jreback jreback added this to the 0.20.0 milestone Feb 23, 2017
AnkurDedania pushed a commit to AnkurDedania/pandas that referenced this issue Mar 21, 2017
…n MultiIndex

closes pandas-dev#12223
closes pandas-dev#15262

Author: Dr-Irv <[email protected]>

Closes pandas-dev#15478 from Dr-Irv/Issue15262 and squashes the following commits:

15d8433 [Dr-Irv] Address jreback comments
10667a3 [Dr-Irv] Fix types for test
8935068 [Dr-Irv] resolve conflicts
385ca3e [Dr-Irv] BUG: GH pandas-dev#12223, GH pandas-dev#15262. Allow ints for names in MultiIndex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants