python提高运算速度_这一行 Python 代码,如何更快的提高运算速度?

本文通过一个示例展示了Python中使用datetime和pandas进行运算时效率较低的问题,并提出了避免使用loc和iloc以提高运算速度的建议。通过对日期处理和数据操作的优化,可以显著提升代码执行效率。
摘要由CSDN通过智能技术生成

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]:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值