我有一个看起来像这样的日期时间列表
as_of_date
[Timestamp('2018-08-01 00:00:00'),
Timestamp('2018-07-01 00:00:00'),
Timestamp('2018-06-01 00:00:00'),
Timestamp('2018-05-01 00:00:00'),
Timestamp('2018-04-01 00:00:00'),
Timestamp('2018-03-01 00:00:00'),
Timestamp('2018-02-01 00:00:00'),
Timestamp('2018-01-01 00:00:00'),
Timestamp('2017-12-01 00:00:00'),
Timestamp('2017-11-01 00:00:00'),
Timestamp('2017-10-01 00:00:00'),
Timestamp('2017-09-01 00:00:00')]
我还有一个名为dates
的日期时间列表,第一行是
dates[0]
[Timestamp('2018-08-01 00:00:00'),
Timestamp('2018-08-01 00:00:00'),
Timestamp('2018-07-01 00:00:00'),
Timestamp('2018-07-01 00:00:00'),
Timestamp('2018-06-01 00:00:00'),
Timestamp('2018-05-01 00:00:00'),
Timestamp('2018-04-01 00:00:00'),
Timestamp('2018-04-01 00:00:00'),
Timestamp('2017-11-01 00:00:00'),
Timestamp('2017-10-01 00:00:00'),
Timestamp('2017-10-01 00:00:00'),
Timestamp('2017-08-01 00:00:00')]
现在,我需要设置要使用的类型和数据。我将举一个例子来说明:
as_of_date = [8/18 7/18 6/18 5/18 4/18 3/18 2/18 1/18 12/17 11/17 10/17 9/17]
dates[0] = [8/18 8/18 7/18 7/18 6/18 5/18 4/18 4/18 11/17 10/17 10/17 8/17 7/17]
现在,我需要一个名为paystring的列表列表,在这里我将说明如何计算第一行。
paystring[0][0] = as_of_date[0] - dates[0][0] + 1 = 1
paystring[0][1] = 0 since as_of_date[1] < dates[0][1]
paystring[0][2] = 0 since as_of_date[2] < dates[0][2]
以此类推
工资字符串的第一行应为
1 0 0 0 0 0 0 0 2 2 1 2
这是我尝试过的:
dates = new_ndd.values.tolist()
NDD_days = start.values.tolist()
paystring = []
for i in range(len(as_of_date)):
paystring.append([])
for j in range(len(dates[i])):
if as_of_date[i] < dates[i][j]:
paystring[i].append(0)
elif NDD_days[i].day > 1:
paystring[i].append(((as_of_date[i].month + 12 - dates[i][j].month)) % 12)
else:
paystring[i].append(((as_of_date[i].month + 12 - dates[i][j].month) + 1) % 12)
print(paystring[0])
但是我明白了:
[1, 1, 2, 2, 3, 4, 5, 5, 10, 11, 11, 1, 2]
有人知道如何解决这个问题吗?
**更多详细信息:
这是常规模式:
paystring[0][0] = as_of_date[0] - dates[0][0]
...
paystring[1][0] = as_of_date[0] - dates[1][0]
答案 0 :(得分:2)
我假设您正在使用Timestamp
库中的pandas
。看起来您在循环中进行了不必要的比较,因此我尝试简化一下。
as_of_date = [
Timestamp('2018-08-01 00:00:00'),
Timestamp('2018-07-01 00:00:00'),
Timestamp('2018-06-01 00:00:00'),
Timestamp('2018-05-01 00:00:00'),
Timestamp('2018-04-01 00:00:00'),
Timestamp('2018-03-01 00:00:00'),
Timestamp('2018-02-01 00:00:00'),
Timestamp('2018-01-01 00:00:00'),
Timestamp('2017-12-01 00:00:00'),
Timestamp('2017-11-01 00:00:00'),
Timestamp('2017-10-01 00:00:00'),
Timestamp('2017-09-01 00:00:00')
]
dates = [
[
Timestamp('2018-08-01 00:00:00'),
Timestamp('2018-08-01 00:00:00'),
Timestamp('2018-07-01 00:00:00'),
Timestamp('2018-07-01 00:00:00'),
Timestamp('2018-06-01 00:00:00'),
Timestamp('2018-05-01 00:00:00'),
Timestamp('2018-04-01 00:00:00'),
Timestamp('2018-04-01 00:00:00'),
Timestamp('2017-11-01 00:00:00'),
Timestamp('2017-10-01 00:00:00'),
Timestamp('2017-10-01 00:00:00'),
Timestamp('2017-08-01 00:00:00')
],
# ... more rows
]
# initialize empty list of lists to fill up
paystring = [[] for _ in range(len(as_of_date))]
# loop through each row of your dates matrix
for date_row in range(len(dates)):
# loop through each element in your dates row
for date_col in range(len(dates[date_row])):
# assuming `as_of_date` and each of the dates rows are of equal length
if as_of_date[date_col] < dates[date_row][date_col]:
paystring[date_row].append(0)
else:
paystring[date_row].append(
as_of_date[date_col].month - dates[date_row][date_col].month + 1)
print(paystring[0])
然后我得到输出:
[1, 0, 0, 0, 0, 0, 0, 0, 2, 2, 1, 2]
希望这会有所帮助!