Python迭代切片日期字符串列表的最快方法

Python迭代切片日期字符串列表的最快方法,python,string,date,Python,String,Date,我有一个很长的日期字符串列表,例如[2011-01-01]、[2015-05-05]。在n个字符串的列表中,我需要选择第I个字符串并查找字符串I:n的最新日期。我可以做到这一点,但这个过程很慢,需要花费数小时才能得到数十万个字符串的列表我缺少哪些代码优化?下面是示例代码 import numpy as np d = np.random.choice(xrange(0, 1000), size=100000, replace=True).tolist() d = [str(item) for i

我有一个很长的日期字符串列表,例如[2011-01-01]、[2015-05-05]。在n个字符串的列表中,我需要选择第I个字符串并查找字符串I:n的最新日期。我可以做到这一点,但这个过程很慢,需要花费数小时才能得到数十万个字符串的列表我缺少哪些代码优化?下面是示例代码

import numpy as np

d = np.random.choice(xrange(0, 1000), size=100000, replace=True).tolist()
d = [str(item) for item in d]

total = len(d)
for i in xrange(total):
    this_slice = d[i:total]
    greatest = max(this_slice)
    if i % 1000 == 0:  # To track progress
        print i 
这些例子进展得很快。使用实际日期字符串(而不是示例中的数字字符串)要慢得多。我已经精确地对执行进行了计时,但是对于600000个日期字符串,似乎需要30-60分钟

以下是我的数据代码的更精确表示:

import pandas as pd

i = 0
rows = df.shape[0]
for date in df['date']:  # date is 'YYYY-MM-DD'
   this_slice = df['date'][i:rows]
   df['new_date'] = max(this_slice)
   if i % 1000 == 0:  # To track progress
       print i
   i += 0

我已将日期字符串转换为datetime对象,使其成为整数(首先删除了“-”),并且速度不会更快。必须有一种更快的方法来编写此代码

如果您从头开始计算,那么算法将更加高效,这样您就可以重用最大值:

import numpy as np

d = np.random.choice(xrange(0, 1000), size=100000, replace=True).tolist()
d = [str(item) for item in d]

total = len(d)
greatest = d[total-1]
for i in reversed(xrange(total)):
    greatest = max(greatest, d[i])
    if i % 1000 == 0:  # To track progress
        print i

如果您从头开始计算,那么该算法将更加高效,这样您就可以重用最大值:

import numpy as np

d = np.random.choice(xrange(0, 1000), size=100000, replace=True).tolist()
d = [str(item) for item in d]

total = len(d)
greatest = d[total-1]
for i in reversed(xrange(total)):
    greatest = max(greatest, d[i])
    if i % 1000 == 0:  # To track progress
        print i

熊猫应该加快速度:

import pandas as pd

df = pd.DataFrame({'date_string': ['2017-01-01', '2011-12-01', '2015-05-05', '2010-10-01']})
df['dates'] = pd.to_datetime(df.date_string)
df['new_date'] = df.dates

for i in range(len(df)):
    df.loc[i, 'new_date'] = df.dates[i:].max()
现在,
df
看起来像这样:

  date_string      dates   new_date
0  2017-01-01 2017-01-01 2017-01-01
1  2011-12-01 2011-12-01 2015-05-05
2  2015-05-05 2015-05-05 2015-05-05
3  2010-10-01 2010-10-01 2010-10-01

熊猫应该加快速度:

import pandas as pd

df = pd.DataFrame({'date_string': ['2017-01-01', '2011-12-01', '2015-05-05', '2010-10-01']})
df['dates'] = pd.to_datetime(df.date_string)
df['new_date'] = df.dates

for i in range(len(df)):
    df.loc[i, 'new_date'] = df.dates[i:].max()
现在,
df
看起来像这样:

  date_string      dates   new_date
0  2017-01-01 2017-01-01 2017-01-01
1  2011-12-01 2011-12-01 2015-05-05
2  2015-05-05 2015-05-05 2015-05-05
3  2010-10-01 2010-10-01 2010-10-01

由于您是按严格的顺序在外部循环的列表上迭代的,因此您可以在剩余的片段中保留最大日期的索引,直到通过为止,从而避免每次调用max。注意:argmax需要整数或浮点数,因此请事先转换日期

 rows = df.shape[0]
 max_remaining_idx = -1
 for i in xrange(rows):  # date is 'YYYY-MM-DD'
     if i > max_remaining_idx:
        max_remaining_idx = df['date'][i:].argmax()
     df['new_date'] = df['date'][max_remaining_idx]
     if i % 1000 == 0:  # To track progress
         print i

由于您是按严格的顺序在外部循环的列表上迭代的,因此您可以在剩余的片段中保留最大日期的索引,直到通过为止,从而避免每次调用max。注意:argmax需要整数或浮点数,因此请事先转换日期

 rows = df.shape[0]
 max_remaining_idx = -1
 for i in xrange(rows):  # date is 'YYYY-MM-DD'
     if i > max_remaining_idx:
        max_remaining_idx = df['date'][i:].argmax()
     df['new_date'] = df['date'][max_remaining_idx]
     if i % 1000 == 0:  # To track progress
         print i

你能使用数据库吗?看起来你在和熊猫一起工作。对吗?将它们放入SQLite并使用您最喜欢的orm。您可以使用数据库吗?看起来您正在使用pandas。对吗?把它们塞进SQLite,然后用你最喜欢的orm。实际上,@Oliver Pellier Cuit的方法更好。实际上,@Oliver Pellier Cuit的方法更好。我用的是熊猫,很抱歉,我的问题的第一稿中不清楚。熊猫在这个循环中仍然很慢,因为它是从顶部切下来的。我用的是熊猫,很抱歉,我的问题的第一稿中不清楚这一点。熊猫在这个循环中仍然很慢,因为它是从顶部切下来的。