Skip to content

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

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
DGalt opened this issue Apr 26, 2016 · 6 comments · Fixed by #30222
Closed

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

DGalt opened this issue Apr 26, 2016 · 6 comments · Fixed by #30222
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@DGalt
Copy link

DGalt commented Apr 26, 2016

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

@jreback
Copy link
Contributor

jreback commented Apr 26, 2016

This works on master for me, I believe was closed on: #12702

are you setting anything in particular, e.g. .max_rows?

@jreback jreback added Output-Formatting __repr__ of pandas objects, to_string MultiIndex labels Apr 26, 2016
@DGalt
Copy link
Author

DGalt commented Apr 26, 2016

No, using whatever is default

@jreback
Copy link
Contributor

jreback commented Apr 26, 2016

all that said, @DGalt would you care to do a PR for validation tests of the 3 examples above?

@jreback jreback added the Testing pandas testing functions or related to the test suite label Apr 26, 2016
@jreback jreback added this to the 0.18.2 milestone Apr 26, 2016
@jreback
Copy link
Contributor

jreback commented Apr 26, 2016

Issues 13000! whoo hoo!

@DGalt
Copy link
Author

DGalt commented Apr 26, 2016

Ha, what a nice round number. And sure, I can put together a PR for those tests, although looking through it's not clear to me where they should go.

@jreback
Copy link
Contributor

jreback commented Apr 26, 2016

put them in tests/test_format.py/test_truncate_with_different_dtypes

@jorisvandenbossche jorisvandenbossche modified the milestones: 0.20.0, 0.19.0 Aug 29, 2016
@jreback jreback modified the milestones: 0.20.0, Next Major Release Mar 23, 2017
@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed MultiIndex Output-Formatting __repr__ of pandas objects, to_string Testing pandas testing functions or related to the test suite labels Oct 7, 2019
@jreback jreback modified the milestones: Contributions Welcome, 1.0 Dec 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants