-1

I have to check what dates are missing for equity options and I want to do this with a for-loop. However

"'(slice(0, 155, None), slice(None, None, None))' is an invalid key"

shows up as error and I can't figure out why.

First it was a datetime64[ns] type and I've already tried to convert it to a string. However, it didn't seem to help. Here is my code :

def days(data, alldays):
days = np.zeros((len(data),1))
companies = ["AAPL", "AXP", "BA", "CAT", "CSCO", "CVX", "DIS", "DJX", "GS", "HD", "IBM", "INTC", "JNJ", \
    "JPM", "KO", "MCD", "MMM", "MRK", "MSFT", "NKE", "PFE", "PG", "TRV", "UNH", "UTX", "V", "VZ", "WBA", "WMT", "XOM"]
d = {}
for i, firm in enumerate(data):
    firms = data[i]
    days[i] = len(firms.date.unique())
    firmdays = firms.date.unique()
    counter = 0
    if days[i] < 3730:
        print(firms.ticker.unique(), len(firms), days[i])
    elif days[i] < len(alldays) and days[i]>=3730:
        d[companies[i]] = np.zeros((582348,6))
        breakpoint()
        for j in range(3733):
            breakpoint()
            if alldays[j] not in  firmdays:
                breakpoint()
                print(firms.ticker.unique(), alldays[j])
                for k, data in enumerate(np.zeros((582348,6))):
                    d[companies[i]][k:k+155,:] = firms[k-156 - counter*156:k-1- 156*counter,:]
                counter = counter +1
            else:
                for k, data in enumerate(np.zeros((582348,6))):
                    d[companies[i]][k:k+155,:] = firms[k- counter*156:k+155 - 156*counter,:]
    print(firms.ticker.unique(), len(firms), days[i])
return days

It gives an error at line "if alldays[j] not in firmdays:", namely

'(slice(0, 155, None), slice(None, None, None))' is an invalid key
Hiro
  • 1
  • 2
  • 1
    what's the value of `alldays[j]` and it's type? – bagerard Jun 01 '19 at 09:20
  • Sorry, the code is a bit lacking. alldays is currently a datetime64[ns] type which basically just gives the date, alldays[0] gives: numpy.datetime64('1996-04-01:00:00.000000000') – Hiro Jun 01 '19 at 09:28
  • and whats the value of `alldays[j]` when the error occurs? Or does it fail on the first iteration (when j=0) – bagerard Jun 01 '19 at 09:30
  • I'm a bit new in Python, but I think it's 'ndarray object of numpy module'. The if statement indeed already fails for j=0 – Hiro Jun 01 '19 at 09:34

1 Answers1

0

Looking at the error, the problem seems more with the use of the slice notation (on part before = sign) on that line:

d[companies[i]][k:k+155,:] = firms[k- counter*156,:]

Not sure what you are trying to achieve but have a look at this post: https://stackoverflow.com/a/509295/6203472

bagerard
  • 5,681
  • 3
  • 24
  • 48
  • Thanks, this might be wrong as well. However, in my real code I have breakpoint() after the if statement, and it doesn't reach this breakpoint due to this line. – Hiro Jun 01 '19 at 09:59
  • please post the full stacktrace that comes with the error then – bagerard Jun 01 '19 at 10:21