根据日期时间查找索引

时间:2015-07-15 11:54:40

标签: python numpy pandas

假设我有以下数据

import numpy as np, pandas as pd

numids = 3
start, end = datetime.datetime(2015, 1, 1), datetime.datetime(2016, 2, 15)
date_list = np.tile(pd.date_range(start, end, freq='B'), numids)

如何查找所有指数,例如date_list == datetime.datetime(2015, 3, 31)?这应该是一个简单的问题,但我似乎无法弄明白。谢谢,Tingis

修改 如果我们采用一个简单示例并将numids设置为1并将感兴趣的日期设置为datetime.datetime(2015, 1, 1),我希望得到索引1。或列表[True, False, False, ..., False]

编辑2 现在,我尝试循环遍历数组并计算天数差异,例如

for date in date_list:
    print(date - datetime.datetime(2015, 1, 12))

然而,这给了我一个我从未见过的错误

TypeError: ufunc subtract cannot use operands with types dtype('<M8[ns]') and dtype('O')

1 个答案:

答案 0 :(得分:2)

你可以试试这个 -

    [
        {
            "Location":"DLI",
            "20FR":"0",
            "20GP":"6",
            "20HC":"0",
            "20HD":"0",
            "20OT":"0",
            ..,,
            ..,,
            "Total":"7",
            "Cost":"251500.000"
        },
        {
            "Location":"LKCMB",
            "20FR":"0",
            "20GP":"0",
            "20HC":"0",
            "20HD":"0",
            "20OT":"0",
            ..,,
            ..,,
            "Total":"9",
            "Cost":"360000.000"
        },
        {
            "Location":"MAA",
            "20FR":"0",
            "20GP":"12",
            "20HC":"0",
            "20HD":"0",
            "20OT":"1",
            ..,,
            ..,,
            "Total":"20",
            "Cost":"1041183.000"
        }
    etc.,
    ]