Skip to content

printing multi-index dataframe with renamed index produces traceback #13000

Closed
@DGalt

Description

@DGalt

When generating a multi-indexed dataframe, if the default index is renamed and the dataframe is longer than the max_rows display option. This cropped up after updating from 0.17 to 0.18.

Code Sample, a copy-pastable example if possible

>>> df = pd.DataFrame({'Vals':range(100)})
>>> df = pd.concat([df, ], keys=['Sweep'], names=['Sweep', 'Index'])
>>> df
Traceback (most recent call last):
  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\indexes\range.py", line 124, in _simple_new
    return RangeIndex(start, stop, step, name=name, **kwargs)
  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\indexes\range.py", line 73, in __new__
    start = _ensure_int(start, 'start')
  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\indexes\range.py", line 59, in _ensure_int
    new_value = int(value)
TypeError: only length-1 arrays can be converted to Python scalars

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\base.py", line 63, in __repr__
    return str(self)
  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\base.py", line 42, in __str__
    return self.__unicode__()
  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\frame.py", line 534, in __unicode__
    line_width=width, show_dimensions=show_dimensions)
  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\frame.py", line 1486, in to_string
    show_dimensions=show_dimensions)
  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\format.py", line 378, in __init__
    self._chk_truncate()
  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\format.py", line 450, in _chk_truncate
    frame.iloc[-row_num:, :]))
  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\tools\merge.py", line 834, in concat
    copy=copy)
  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\tools\merge.py", line 972, in __init__
    self.new_axes = self._get_new_axes()
  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\tools\merge.py", line 1059, in _get_new_axes
    new_axes[self.axis] = self._get_concat_axis()
  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\tools\merge.py", line 1111, in _get_concat_axis
    concat_axis = _concat_indexes(indexes)
  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\tools\merge.py", line 1129, in _concat_indexes
    return indexes[0].append(indexes[1:])
  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\indexes\multi.py", line 1029, in append
    label = self.get_level_values(i)
  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\indexes\multi.py", line 666, in get_level_values
    tz=getattr(unique, 'tz', None))
  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\indexes\range.py", line 126, in _simple_new
    return Index(start, stop, step, name=name, **kwargs)
  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\indexes\base.py", line 214, in __new__
    return Int64Index(data, copy=copy, dtype=dtype, name=name)
  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\indexes\numeric.py", line 107, in __new__
    not issubclass(np.dtype(dtype).type, np.integer)):
TypeError: data type "Index" not understood

Expected Output

The expected outcome is that the dataframe should print out without producing a traceback, as occurs when the length is less than that set by max_rows. I.e.

>>> df = pd.DataFrame({'Vals':range(10)})
>>> df = pd.concat([df, ], keys=['Sweep'], names=['Sweep', 'Index'])
>>> df
             Vals
Sweep Index
Sweep 0         0
      1         1
      2         2
      3         3
      4         4
      5         5
      6         6
      7         7
      8         8
      9         9

The issue only seems to be with changing the default index name, as you can change the other index name without issue:

>>> df = pd.DataFrame({'Vals':range(100)})
>>> df = pd.concat([df, ], keys=['Sweep'], names=['Sweep',])
>>> df
          Vals
Sweep
Sweep 0      0
      1      1
      2      2
      3      3
      4      4
      5      5
      6      6
      7      7
      8      8
      9      9
      10    10
      11    11
      12    12
      13    13
      14    14
      15    15
      16    16
      17    17
      18    18
      19    19
      20    20
      21    21
      22    22
      23    23
      24    24
      25    25
      26    26
      27    27
      28    28
      29    29
...        ...
      70    70
      71    71
      72    72
      73    73
      74    74
      75    75
      76    76
      77    77
      78    78
      79    79
      80    80
      81    81
      82    82
      83    83
      84    84
      85    85
      86    86
      87    87
      88    88
      89    89
      90    90
      91    91
      92    92
      93    93
      94    94
      95    95
      96    96
      97    97
      98    98
      99    99

[100 rows x 1 columns]

output of pd.show_versions()

>>> pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.5.1.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None

pandas: 0.18.0
nose: 1.3.7
pip: 8.1.0
setuptools: 20.2.2
Cython: 0.23.4
numpy: 1.10.4
scipy: 0.17.0
statsmodels: 0.6.1
xarray: None
IPython: 4.0.3
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.6
matplotlib: 1.5.1
openpyxl: 2.3.2
xlrd: 0.9.4
xlwt: 1.0.0
xlsxwriter: 0.8.4
lxml: 3.5.0
bs4: 4.4.1
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.11
pymysql: None
psycopg2: None
jinja2: 2.8
boto: 2.39.0

Discussed in more detail here

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