pythonarrayspandasnumpytypeerror

Code won't work when looping through directory: TypeError: dispatcher for __array_function__ did not return an iterable


I'm trying to create a boolean column ('flag_rise') that marks instances where numbers are increasing in a different column ('Zoom')-so marking it as True when a number is larger than the number before it. The code I wrote works when I run it on one file, but when I try to run it through the directory, I get this error:

TypeError: dispatcher for array_function did not return an iterable.

I can't find much about this error online. I've double checked the files by running the code on a subset of files, and I got the same error. The files are all formatted the same way. Any help is appreciated.

Here is the code:

# set directory
directory = os.chdir(r"directory")

# create list of files
dir_list = os.listdir(directory)

for file in dir_list:
    def rise (a):
        return np.concatenate((False),a[1:] > a[:-1])
    df['flag_rise'] = rise(df.Zoom.values)

Solution

  • concatenate expects a list of arrays (or at least an iterable) as the first argument. The second is supposed to be an integer, an axis.

    In [421]: np.concatenate((False),np.array([True, False]))
    Traceback (most recent call last):
      File "<ipython-input-421-2e0a61c32e56>", line 1, in <module>
        np.concatenate((False),np.array([True, False]))
      File "<__array_function__ internals>", line 5, in concatenate
    TypeError: dispatcher for __array_function__ did not return an iterable
    

    The first argument is (False). The () do nothing here, this is just False, not a list, tuple or other iterable. The second argument is a array.

    Changing the brackets to wrap both False and the array is not enough, because False is still a scalar (or 0d array):

    In [422]: np.concatenate([False,np.array([True, False])])
    Traceback (most recent call last):
      File "<ipython-input-422-f6c715674042>", line 1, in <module>
        np.concatenate([False,np.array([True, False])])
      File "<__array_function__ internals>", line 5, in concatenate
    ValueError: zero-dimensional arrays cannot be concatenated
    

    Make False a list, then it works:

    In [423]: np.concatenate([[False],np.array([True, False])])
    Out[423]: array([False,  True, False])
    

    concatenate converts all those elements of the list to arrays:

    In [425]: np.array(False)
    Out[425]: array(False)
    In [426]: _.shape
    Out[426]: ()
    In [427]: __.ndim
    Out[427]: 0
    

    It can't join a () shape array to a (2,) shape on axis 0. The arrays have to match in the number of dimensions.