only one length arrays but python not recognizing that it is

Yesterday this code worked perfectly fine but now im getting this error

only length-1 arrays can be converted to Python scalars.

import os
import re
import mouse 
import time
import numpy 
import pandas as pd
import keyboard as kb
import subprocess as sub
import pyautogui as pyAuto

from re import findall
from numpy import ndim
from pathlib import Path
from numpy import ndarray


stockNumber = xlEntries['StockNumber']  # Column of stock numbers


# While loop to find the row numbers and the the specification name and value of each of the desired row numbers
j = 0 # Reset count for while loop
while (j<fileCount):
        rowNumbertemp = xlEntries[stockNumber == partNumbers[j]].index.to_numpy()
        rowNumber.append(int(rowNumbertemp))
        specNametemp = xlEntries.loc[rowNumber[j],"Pat's Proposed Change"]
        specName.append(specNametemp)
        specValuetemp = xlEntries.loc[rowNumber[j],"StringValue"]
        specValue.append(specValuetemp)
        j = j + 1

This is the full error and traceback:

Traceback (most recent call last):
  File "c:\Users\jmcintosh\LIDT PRoject (copy)\LIDT Project\NEW EDMUND OPTICS LIDT\LIDT_MULTIPLE_FILE_TEST.py", line 59, in <module>
    rowNumber.append(int(rowNumbertemp))
                     ^^^^^^^^^^^^^^^^^^
TypeError: only length-1 arrays can be converted to Python scalars

There is some other code i didnt include but it doesnt apply to this. I apologize if this question isnt formatted right im vey new to SO.

I tried removing the int but it messed with the info im trying to parse using pandas.im at a los since it has been working for days and now its not.

>Solution :

The error message "only length-1 arrays can be converted to Python scalars" usually occurs when you are trying to use a NumPy array where a scalar value is expected.

The variable rowNumbertemp is a NumPy array, and you are trying to convert it to an integer using int(). However, you should not convert a NumPy array to an integer directly, especially if it contains multiple values.

Fast Solution:
In this approach, the for loop goes through each element of the rowNumbertemp array and appends each element separately as an integer to the rowNumber list.

rowNumbertemp = xlEntries[stockNumber == partNumbers[j]].index.to_numpy()
for row in rowNumbertemp:
    rowNumber.append(int(row))

You may fix it quickly.

Leave a Reply