Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12201

Running into a TypeError with openpyxl Series and unsure why

$
0
0

I am having an issue with using series in openpyxl. This is my code, which takes in a dictionary of dataframes and creates a filtered dataframe based off input parameters. My error occcurs when I try to make a new series, and I get the error message below. I am not sure what I am doing wrong. My Reference ranges are correct.

raise TypeError('expected '+ str(expected_type))TypeError: expected <class 'int'>

Here is the code:

def create_chart(workbook, dfs, sheet_name, key, y_label):"""Generate chart from the dataframe(s)."""    # create the chart    chart = openpyxl.chart.ScatterChart()    chart.title = sheet_name    chart.x_axis.title = "X values"    chart.y_axis.title = y_label    start_row = 1    start_col = 1    workbook.save("example.xlsx")    for material, df in filtered_dfs.items():        x_values = Reference(ws, min_col=start_col, min_row=start_row+2,                                      max_row=start_row+1+len(df))        y_values = Reference(ws, min_col=start_col+1, min_row=start_row + 2,                                      max_row=start_row + 1 + len(df))        new_series = series.Series(y_values, x_values)        workbook.save("example.xlsx")        start_col += len(df.columns) + 1    ws.add_chart(chart, "H6")def write_filtered_dfs_to_excel_sheet(filtered_dfs, ws):"""Write specified dataframe to column and separate."""    # write graph to sheet    start_row = 1    start_col = 1    for material, df in filtered_dfs.items():        # write header for each material        ws.cell(row=start_row, column=start_col, value=material)        # write column headers        for i, col in enumerate(df.columns):            ws.cell(row=start_row + 1, column=start_col + i, value=col)        # going through each row, column in df and appending to excel        for row_idx, row in df.iterrows():            for col_idx, value in enumerate(row):                ws.cell(row=start_row + 2 + row_idx, column=start_col + col_idx, value=float(value))        start_col += len(df.columns) + 1

I have error checked it to make sure the ranges and values are correct, and they seem to be. The data is also written correctly in the Excel file before I access it.Here is an example of the dataframes I would be using.

df = pd.DataFrame({'X': [0.0000, 3.5714, 7.1429, 10.7143, 14.2857, 17.8571, 21.4286, 25.0000],'Y': [0.00000000, 0.14285714, 0.28571429, 0.42857143, 0.57142857, 0.71428571, 0.85714286, 1.00000000]})

Viewing all articles
Browse latest Browse all 12201

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>