熊猫根据其他两个具有日期时间值的列创建一个布尔列

时间:2019-06-27 11:40:44

标签: python python-3.x pandas dataframe

我有一个df

date1        date2
2019-05-31   2019-06-01
NaT          NaN
2018-07-01   2018-08-01
NaT          2019-06-03
2019-01-01   NaN

如果on_time-3 <= date2 - date1 <= 0中的任何值为date1date2,我想基于NaN创建一个布尔列NaT ,制作on_time = False;

    a = df['date1'].isna()
    b = df['date2'].isna()

    df['on_time'] = (a | b)

    m = (-3 <= (df.loc[~a&~b, 'date1'] - df.loc[~a&~b, 'date2']).dt.days) & \
        ((df.loc[~a&~b, 'date1'] - df.loc[~a&~b, 'date2']).dt.days <= 0)

    df['on_time'] = m

我想知道是否有更好的方法,更简洁,更有效的方法。

2 个答案:

答案 0 :(得分:3)

IIUC,您可以使用series.dt.days()创建一个帮助器系列,并使用s.ge()le进行比较:

s=(df.date2-df.date1).dt.days
df=df.assign(on_time=s.ge(-3)&s.le(0))

       date1      date2  on_time
0 2019-05-31 2019-06-01    False
1        NaT        NaT    False
2 2018-07-01 2018-08-01    False
3        NaT 2019-06-03    False
4 2019-01-01        NaT    False

答案 1 :(得分:1)

public TransactionResponse voidTransaction(PaymentInstrument _paymentInstrument, ServiceContext serviceContext) {
     ...................
     try {
            ResponseEntity<TransactionResponse> responseEntity = restTemplate.exchange(uri, HttpMethod.PUT, requestEntity, TransactionResponse.class);
            voidResponse = responseEntity.getBody();
            log.info(LogSupport.PG_LOGS_CARGILLS_VOID_TRX + "[AcquirerMID ={};TransactionId ={};ResponseCode={};ResponseText={}]", _paymentInstrument.getAcquirerMid(), 
                    _paymentInstrument.getTransactionId(), voidResponse.getResponse().getAcquirerCode(), voidResponse.getResponse().getAcquirerMessage());
        }catch (Exception exception){
            if(exception instanceof ResourceAccessException){
                log.error(LogSupport.PG_LOGS_CARGILLS_VOID_TRX+" Exception: {}", exception.getMessage());
            }
            log.error(LogSupport.PG_LOGS_CARGILLS_VOID_TRX+" Exception :{} ", exception.getMessage());
            voidResponse = new TransactionResponse();
            voidResponse.setResult("ERROR");
            ErrorResponse error = new ErrorResponse();
            error.setCause("EXCEPTION");
            error.setExplanation(exception.getMessage());
            voidResponse.setError(error);

        }
 }

public Response VOid(){
     try {
            transactionResponse = cargillsIpgProxy.voidTransaction(paymentInstrument,serviceContext);
            log.info(LogSupport.OTC_VOID_TRANSACTION + "[|CustomerId:{}|TransactionId:{}|{}|Transaction response:{}]",
                axiPayCargills.getCustomerId(), axiPayCargills.getCustomerPayeeTransactionId(), uniqueUUID, transactionResponse.toString());
          } catch (Exception exception) {
            log.info(LogSupport.OTC_VOID_TRANSACTION + "[|CustomerId:{}|TransactionId:{}|{}|Void error:{}]",
                axiPayCargills.getCustomerId(), axiPayCargills.getCustomerPayeeTransactionId(), uniqueUUID, exception.getMessage());
// Here i want to return above method setted result value

          }
}

输出

## if the dates are of type str
df['date1'] = pd.to_datetime(df['date1'])
df['date2'] = pd.to_datetime(df['date2'])


(df['date2'] - df['date1']).apply(lambda x: True if -3<= x.days <=0   else False)