python3迭代日期

时间:2017-11-02 14:26:47

标签: python-3.x datetime iteration timedelta period

我有一个日期对象列表X(“%Y,%m,%d”)和一个日期Y,并希望有一个列表Z,其中包含距离日期+ 10天的所有日期Y忽略年。

Ex.: 
timeDelta = 10days
X = [2017-10-10, 2014-09-31, 1999-05-10, 1992-10-18]
Y = 2019-10-05
Z = MyFunction(X, Y, delta)


MyFunction(X,Y, timeDelta):
for i in range((currentDate-500000),currentDate,10000):  # last 50 years in 1 year steps
     bottomBoundries.append(datetime.strptime(str(i),'%Y%m%d')-timedelta(timeDelta))
     topBoundries.append(datetime.strptime(str(i),'%Y%m%d')+timedelta(timeDelta))
 for i in range(0, pandas.shape[0], 1):
     for j in range(0, len(bottomBoundries) , 1):
            if ((pandas["MESS_DATUM"].iloc[i] > bottomBoundries[j]) & (pandas["MESS_DATUM"].iloc[i] < topBoundries[j])):
                indices.append(i)
 result = pandas.iloc[indices]
return result

这需要花费很多时间,我想知道如何提高效率。

2 个答案:

答案 0 :(得分:0)

您可以使用timetuple().yday来获取当年的一天

所以这样的事情可能有用:

def diff_without_year(date, reference_date, timedelta):
    ref_yday = reference_date.timetuple().yday
    diff = d.timetuple().yday - ref_yday # + optionally code to take care of the difference in hours, minutes,...
    return abs(diff) <= timedelta

这里timedelta就是天。如果您在TimeDelta对象中获取该对象,则可以通过除以pd.TimeDelta('1day')

进行转换
df['result'] = df["MESS_DATUM"].apply(lambda x: diff_without_year(x, ref_date, timedelta))

如果您的日期属于pandas系列,则可以使用Series.dt.dayofyear

进行简化。
df['result'] = (df["MESS_DATUM"].dt.dayofyear - reference_date.timetuple().yday) < timedelta

答案 1 :(得分:0)

A more adhoc solution that checks for each date in X whether the date in the year of Y is within 10 days, as well as the date in the preceding and succeeding year. This runs in linear time in size of X. For a large list of X, you can adapt this to do it in a pandas dataframe, and parallelize this code.

import datetime

timeDelta = 10  # in days
X = ['2017-10-10', '2014-09-30', '1999-05-10', '1992-10-18']
Y = '2019-10-05'
Y_date = datetime.datetime.strptime(Y, '%Y-%m-%d')  # convert to datetime
td = datetime.timedelta(timeDelta)
year = Y_date.year

output_dates = []
for date in X:
    X_date = datetime.datetime.strptime(date, '%Y-%m-%d')
    month = X_date.month
    day = X_date.day

    date_previous_year = datetime.datetime(year=year-1, month=month, day=day)
    date_current_year = datetime.datetime(year=year, month=month, day=day)
    date_next_year = datetime.datetime(year=year+1, month=month, day=day)
    if abs(date_previous_year - Y_date) <= td or \
        abs(date_current_year - Y_date) <= td or \
        abs(date_next_year - Y_date) <= td:
        output_dates.append(date)
print(output_dates)