一个在b中的python,一个不在b中的py

时间:2015-11-10 17:05:00

标签: python numpy

我有一个numpy数组的datetime对象。我的想法是我必须使用for循环中的唯一日期来做事情。有些东西让我很难过。问题如下所示。我做一个for循环,for d in np.unique(dates):。但是在for循环中我希望d in np.unique(dates)d in dates返回True,但每次都返回false?

我用它来说明问题。

import numpy as np    

....
print type(dates[0])
print dates[0]
print 'start for loop -----'
for d in np.unique(dates):
    print type(d)
    print 'd: ',d
    print 'd in np.unique(dates): ',d in np.unique(dates)
    print 'd in dates',d in dates

输出:

<type 'numpy.datetime64'>
2010-05-22T02:00:00.000000000+0200
start for loop -----
<type 'numpy.datetime64'>
d:  2010-05-22T02:00:00.000000000+0200
d in np.unique(dates):  False
d in dates:  False
<type 'numpy.datetime64'>
d:  2010-07-17T02:00:00.000000000+0200
d in np.unique(dates):  False
d in dates:  False
....

1 个答案:

答案 0 :(得分:1)

As the others i am not able to reproduce the error. Here is my implementation of your code that forked absolutely fine. I am using Ubuntu 15.04 with python 3.5 and numpy 1.10.1

As the others i am not able to reproduce the error. But maybe it helps here is my implementation of your code. If you provide more info about your system -- maybe it helps. I am using Ubuntu 15.04

import numpy as np import sys import platform print("Arch:", platform.architecture()) print("Python version:",".".join([str(v) for v in sys.version_info[0:3]])) print("numpy version:",np.version) Arch: ('64bit', 'ELF') Python version: 3.5.0 numpy version: 1.10.1

dates = np.arange('2015-02', '2015-03', dtype='datetime64[D]')
print(dates)
#prints
['2015-02-01' '2015-02-02' '2015-02-03' '2015-02-04' '2015-02-05'
 '2015-02-06' '2015-02-07' '2015-02-08' '2015-02-09' '2015-02-10'
 '2015-02-11' '2015-02-12' '2015-02-13' '2015-02-14' '2015-02-15'
 '2015-02-16' '2015-02-17' '2015-02-18' '2015-02-19' '2015-02-20'
 '2015-02-21' '2015-02-22' '2015-02-23' '2015-02-24' '2015-02-25'
 '2015-02-26' '2015-02-27' '2015-02-28']

print(type(dates[0]))
print(dates[0])
print('start for loop -----')
for d in np.unique(dates):
    print (type(d))
    print ('d: ',d)
    print ('d in np.unique(dates): ',d in np.unique(dates))
    print ('d in dates',d in dates)

#prints 
<class 'numpy.datetime64'>
2015-02-01
start for loop -----
<class 'numpy.datetime64'>
d:  2015-02-01
d in np.unique(dates):  True
d in dates True

...