Skip to content

Commit

Permalink
Work around for shape issues with to_dataframe
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottDelbecq committed Feb 6, 2025
1 parent 76d02ba commit cc96ecb
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/fastoad/gui/variable_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,23 +209,22 @@ def _sheet_to_df(sheet: sh.Sheet) -> pd.DataFrame:
:param sheet: the ipysheet Sheet to be converted
:return the equivalent pandas DataFrame
"""
df = sh.to_dataframe(sheet)
# data = []
# for cell in sheet.cells:
# if cell.row_end - cell.row_start == 0 and cell.column_end - cell.column_start == 0:
# data.append(cell.value)
# else:
# data.extend([cell.value] * (cell.row_end - cell.row_start + 1))
#
# # Reshape data to match the number of columns
# num_columns = len(sheet.column_headers)
# reshaped_data = [data[i:i + num_columns] for i in range(0, len(data), num_columns)]
#
# # Ensure reshaped_data has the correct number of rows
# if len(reshaped_data[-1]) != num_columns:
# reshaped_data.pop()
#
# df = pd.DataFrame(reshaped_data, columns=sheet.column_headers)
data = []
for cell in sheet.cells:
if cell.row_end - cell.row_start == 0 and cell.column_end - cell.column_start == 0:
data.append(cell.value)
else:
data.extend([cell.value] * (cell.row_end - cell.row_start + 1))

# Reshape data to match the number of columns
num_columns = len(sheet.column_headers)
reshaped_data = [data[i : i + num_columns] for i in range(0, len(data), num_columns)]

# Ensure reshaped_data has the correct number of rows
if len(reshaped_data[-1]) != num_columns:
reshaped_data.pop()

df = pd.DataFrame(reshaped_data, columns=sheet.column_headers)
return df

# pylint: disable=unused-argument # args has to be there for observe() to work
Expand Down

0 comments on commit cc96ecb

Please sign in to comment.