简体   繁体   English

使用蒙版设置numpy ndarray的值

[英]Setting values for a numpy ndarray using mask

I want to calculate business days between two times, both of which contain null values, following this question related to calculating business days. 我想计算两次之间的工作日,两次都包含空值, 这个问题与计算工作日有关。 I've identified that the way I'm setting values using a mask does not behave as expected. 我已经确定我使用掩码设置值的方式不符合预期。

I'm using python 2.7.11, pandas 0.18.1 and numpy 1.11.0. 我正在使用python 2.7.11,pandas 0.18.1和numpy 1.11.0。 My slightly modified code: 我稍加修改的代码:

import datetime
import numpy as np
import pandas as pd

def business_date_diff(start, end):
    mask = pd.notnull(start) & pd.notnull(end)
    start = start[mask]
    end = end[mask]
    start = start.values.astype('datetime64[D]')
    end = end.values.astype('datetime64[D]')
    result = np.empty(len(mask), dtype=float)
    result[mask] = np.busday_count(start, end)
    result[~mask] = np.nan
    return result

Unfortunately, this doesn't return the expected business day differences (instead I get a number of very near 0 floats). 不幸的是,这并没有返回预期的工作日差异(相反,我获得了一些非常接近的0浮点数)。 When I check np.busday_count(start, end) the results look correct. 当我检查np.busday_count(start, end) ,结果看起来是正确的。

print start[0:5]
print end[0:5]
print np.busday_count(start, end)[0:5]

# ['2016-07-04' '2016-07-04' '2016-07-04' '2016-07-04' '2016-07-04']
# ['2016-07-05' '2016-07-05' '2016-07-05' '2016-07-06' '2016-07-06']
# [1 1 1 2 2]

But when I check the values for results the results do not make sense: 但是,当我检查results的值时, results没有意义:

...
result = np.empty(len(mask), dtype=float)
result[mask] = np.busday_count(start, end)
result[~mask] = np.nan
print result

# [           nan               nan   1.43700866e-210   1.45159738e-210
# 1.45159738e-210   1.45159738e-210   1.45159738e-210   1.46618609e-210
# 1.45159738e-210   1.64491834e-210   1.45159738e-210   1.43700866e-210
# 1.43700866e-210   1.43700866e-210   1.43700866e-210   1.45159738e-210
# 1.43700866e-210   1.43700866e-210   1.43700866e-210   1.43700866e-210

What am I doing wrong? 我究竟做错了什么?

Your problem is that with your version of numpy, you can't use a boolean array as an index to an array. 您的问题是,对于您的numpy版本,您不能使用布尔数组作为数组的索引。 Just use np.where(mask==True) instead of mask and np.where(mask==False) instead of ~mask, and it will work as desired. 只需使用np.where(mask==True)代替mask和np.where(mask==False)而不是~mask,它将按照需要工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM