ipysheet icon indicating copy to clipboard operation
ipysheet copied to clipboard

pandas loader to dataframe list index out of range

Open jhzsquared opened this issue 3 years ago • 1 comments

When trying to use pandas_loader.to_dataframe with an incompletely filled sheet, I get a IndexError: list index out of range . This is because a Cell perhaps with values of row start and end being 1, and column of 1 will only have one value in the value list, but when calling that value the extract_cell_data goes to value[1]. This only happens (I think) because when defining the sheet, I defined my first row as a list and then defined the other cells via the column.

I modified my code with the following replacement to extract_cell_data to partially fix this.

def extract_cell_data_new(cell, data):
    value = cell.value
    if cell.row_start == cell.row_end:
        col = cell.column_start
        row = cell.row_start
        for v in value:
            data[row][col]['value'] = v
            data[row][col]['options']['type'] = cell.type
            col = col+1
    else:
        row = cell.row_start
        col = cell.column_start
        for v in value:
            data[row][col]['value'] = v
            data[row][col]['options']['type'] = cell.type
            row = row+1
    

jhzsquared avatar Oct 13 '20 15:10 jhzsquared

Exactly the same problem. Just using an intuitive way to create a sheet, populate first row with headers then fill with data. Toggle the commented line to replicate the error.

from ipysheet import sheet, cell, row, column
from ipysheet import to_dataframe
sheet1 = sheet(rows=6, columns=4)
row = row(0, list(map(chr, range(ord('a'), ord('d')+1))))
column = column(0, [*range(5)] ,row_start=1)
#column = column(0,['a'] + [*range(5)] ,row_start=0)

sheet1

df = to_dataframe(sheet1)
df

pdavidsonFIA avatar Oct 14 '21 06:10 pdavidsonFIA