EmdeBoas
2017-05-13 23:59:51 +08:00
老哥 你别老复杂化问题啊.....loc 和 iloc 要少用 效率很低的
In [17]: def test():
...: array = []
...: other = []
...: for i in xrange(1000000):
...: [TAB]other.append(i)
...: [TAB]year = np.random.randint(100) + 2000
...: [TAB]month = np.random.randint(12) + 1
...: [TAB]day = np.random.randint(28) + 1
...: [TAB]array.append(datetime.datetime(year, month, day))
...: narray = np.array(array)
...: flag = datetime.datetime(2015, 5, 3)
...: df = pd.DataFrame()
...: df['date'] = narray
...: df['other'] = other
...: print df[narray[:]<=flag][-5:]
...:
In [18]: %timeit test()
date other
999979 2006-07-05 999979
999980 2012-09-19 999980
999981 2010-05-13 999981
999990 2007-10-14 999990
999996 2008-10-19 999996
date other
999979 2002-08-01 999979
999983 2001-10-01 999983
999984 2007-04-05 999984
999988 2014-04-21 999988
999991 2008-01-06 999991
date other
999977 2004-05-04 999977
999981 2004-05-05 999981
999990 2003-10-04 999990
999991 2003-03-28 999991
999992 2002-12-09 999992
date other
999964 2006-12-13 999964
999970 2012-07-07 999970
999971 2009-12-15 999971
999976 2004-07-22 999976
999982 2009-11-14 999982
1 loop, best of 3: 4.58 s per loop
In [19]: