0

This is related to this SO question: read_excel in pandas giving error for no header and multiple index_col's

But instead of a workaround, I would like to know why this is happening. The data frame:

enter image description here

The data:

{0: {0: nan, 1: nan, 2: nan, 3: 'A', 4: 'A', 5: 'B', 6: 'B', 7: 'C', 8: 'C'},
 1: {0: nan, 1: nan, 2: nan, 3: 1.0, 4: 2.0, 5: 1.0, 6: 2.0, 7: 1.0, 8: 2.0},
 2: {0: 'AA1', 1: 'a', 2: 'ng/mL', 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1},
 3: {0: 'AA2', 1: 'a', 2: nan, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1},
 4: {0: 'BB1', 1: 'b', 2: nan, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1},
 5: {0: 'BB2', 1: 'b', 2: 'mL', 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1},
 6: {0: 'CC1', 1: 'c', 2: nan, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1},
 7: {0: 'CC2', 1: 'c', 2: nan, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1}}

Reading the data like:

pd.read_excel(file_path, skiprows=3, index_col=[0, 1], header=None)

Does not work:

TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

Why?

smci
  • 32,567
  • 20
  • 113
  • 146
Moritz
  • 5,130
  • 10
  • 40
  • 81

1 Answers1

0

The explanation is given in the full traceback:

  ....
  File "D:\Programme\Python36\lib\site-packages\pandas\io\excel\_base.py", line 473, in parse offset = 1 + header
  TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

header is set to None and in calculating the offset it tries to add 1 to None which results in a TypeError. I think it's simply a bug.

The following is absolutely without any warranty:
line 473 of ...\Lib\site-packages\pandas\io\excel_base.py should be changed from offset = 1 + header to offset = 1 + header if header is not None else -1 to make multiple index columns work with header=None

Stef
  • 28,728
  • 2
  • 24
  • 52