我是python的初学者。我试图运行下面的代码,但我在帖子中得到了像标题一样的错误。我读过类似的帖子,但没有解决我的问题。我试图用python 2.7.13和最新的3.6.10运行它。同样的问题。代码如下:
In [795]: SPopen = pd.bdate_range(start = '1950-01-01', end = '2020-01-01')
...: startdates = pd.bdate_range(start = '1970-01-01', end = '2000-01-01')
...: enddates = startdates + pd.Timedelta(1, 'Y')
...:
In [796]: a = SPopen
...: s0 = startdates
...: s1 = enddates
...:
In [797]: out1 = [np.sum( (a > s0[i]) & (a <= s1[i]) ) for i in range(len(s0))]
...: out2 = vectorized_interval_count(a, s0, s1)
...: out3 = np.searchsorted(a,s1,'right') - np.searchsorted(a,s0,'right')
...: print np.allclose(out1, out2)
...: print np.allclose(out1, out3)
...:
True
True
In [798]: %timeit [np.sum( (a > s0[i]) & (a <= s1[i]) ) for i in range(len(s0))]
1 loops, best of 3: 4.44 s per loop
In [799]: %timeit vectorized_interval_count(a, s0, s1)
1000 loops, best of 3: 842 µs per loop
In [800]: %timeit np.searchsorted(a,s1,'right') - np.searchsorted(a,s0,'right')
1000 loops, best of 3: 559 µs per loop