views:

36

answers:

1

Hi Everyone,

I would really appreciate some help on this issue, I haven't been able to find anything about this value error online and I am at a complete loss as to why my code is illiciting this response.

I have a large dictionary of something like 50 keys. The value associated with each key is a 2D array of many elements of the form [a datetime object, and some other info]. A sample would look like this:

{'some_random_key': array([[datetime(2010, 10, 26, 11, 5, 28, 157404), 14.1],
                           [datetime(2010, 10, 26, 11, 5, 38, 613066), 17.2]],
                          dtype=object), 
'some_other_key':  array([[datetime(2010, 10, 26, 11, 5, 28, 157404), 'true'],
                          [datetime(2010, 10, 26, 11, 5, 38, 613066), 'false']], 
                         dtype=object)}

What I want my code to do is to allow a user to select a start and stop date and remove all of the array elements (for all of the keys) that are not within that range.

Placing print statements through out the code I was able to deduce that it can find the dates that are out of range, but for some reason, the error occurs when it attempts to remove the element from the array.

Here is my code:


def selectDateRange(dictionary, start, stop):

    #Make a clone dictionary to delete values from 
    theClone = dict(dictionary)

    starting = datetime.strptime(start, '%d-%m-%Y')   #put in datetime format
    ending   = datetime.strptime(stop+' '+ '23:59', '%d-%m-%Y %H:%M')    #put in datetime format 

    #Get a list of all the keys in the dictionary
    listOfKeys = theClone.keys()

    #Go through each key in the list
    for key in listOfKeys:
        print key 

        #The value associate with each key is an array
        innerAry = theClone[key]

        #Loop through the array and . . .
        for j, value in enumerate(reversed(innerAry)):

            if (value[0] <= starting) or (value[0] >= ending):
            #. . . delete anything that is not in the specified dateRange

                del innerAry[j]

    return theClone

This is the error message that I get:

ValueError: cannot delete array elements

and it occurs at the line "del innerAry[j]"

Please help!!!! Perhaps you have the eye to see the problem where I cannot.

Thanks!

+1  A: 

NumPy arrays are fixed in size. Use lists instead.

Ignacio Vazquez-Abrams
Thanks! I can't believe I didn't find much documentation on that fact... especially since an array object has a .__delitem__ function... confusing. Either way, thanks. I think I'll go ahead and use lists for the deleting.
mshell_lauren
Of course it has a `__delitem__()` method. But as you've discovered, its only purpose is to raise an exception.
Ignacio Vazquez-Abrams