从列表中减去日期,从列表中减去每一行 [英] Subtracting dates from a list and each row from a list of lists

查看:67
本文介绍了从列表中减去日期,从列表中减去每一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的日期时间列表

I have a list of datetimes that looks like this

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的日期时间列表,第一行是

I also have a list of lists of datetimes called dates, the first row is

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')]

现在我需要设置要使用的类型和数据.我将举一个例子来说明:

Now that I set up the types and data I am working from this is what I need to do. I will illustrate with an example:

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的列表列表,在这里我将说明如何计算第一行.

Now I need a list of lists called paystring where I will now illustrate how I calculate the first row.

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]

以此类推

工资字符串的第一行应为

The first row of the paystring should be

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]

有人知道如何解决这个问题吗?

Anyone know how to fix this?

**更多详细信息:

这是常规模式:

paystring[0][0] = as_of_date[0] - dates[0][0]
...
paystring[1][0] = as_of_date[0] - dates[1][0]

推荐答案

我假设您正在使用pandas库中的Timestamp.看起来您在循环中进行了不必要的比较,因此我尝试简化一下.

I am assuming you are using Timestamp from the pandas library. It looks like you are doing unnecessary comparisons in your loops, so I tried simplifying a bit.

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]

希望这会有所帮助!

这篇关于从列表中减去日期,从列表中减去每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆