如何检查日期时间在熊猫的两个日期时间之间

时间:2018-12-03 06:55:24

标签: python pandas

我有以下第一个熊猫数据框

<NavLink>

现在,我有了主价格数据帧,可以根据该数据帧检查 trans_df code price date time product 12023 71.23 01-01-2018 06:23:00 MS 12023 61 01-01-2018 07:56:00 HS 12023 71.23 01-01-2018 08:34:00 MS 12023 71.30 01-01-2018 06:03:00 MS 12023 61 01-01-2018 11:43:00 HS 12023 71.23 01-01-2018 10:11:00 MS 12023 71.23 01-01-2018 04:23:00 MS 12023 72.23 02-01-2018 10:11:00 MS 12023 72.23 02-01-2018 04:23:00 MS 中设置的价格是否正确,并且交易日期和时间在{中的trans_dfeffective_date_from之间{1}}用于该特定产品

effective_date_to

所需的数据框为

master_price

1 个答案:

答案 0 :(得分:3)

使用:

#convert dates with times to datetimes
master_price['effective_date_from'] = (pd.to_datetime(master_price['effective_date_from'], 
                                       format='%d-%m-%Y') + 
                                      pd.to_timedelta(master_price['time_from']))
master_price['effective_date_to'] = (pd.to_datetime(master_price['effective_date_to'], 
                                     format='%d-%m-%Y') + 
                                     pd.to_timedelta(master_price['time_to']))
trans_df['date'] = (pd.to_datetime(trans_df['date'], format='%d-%m-%Y') +
                    pd.to_timedelta(trans_df['time']))

#join together and filter between 
df = trans_df.merge(master_price, on=['code','product'], how='left')
df = df[df.date.between(df.effective_date_from, df.effective_date_to)]

#add only filterd rows to original
df = trans_df.merge(df, on=['code','product','date','time'], how='left')
cols = ['effective_date_from', 'effective_date_to', 'time_to','time_from','price_x']
df = df.drop(cols, axis=1)
#first test missing values then match.mismatch
df['flag'] = np.select([df['price_y'].isnull(), 
                        df['price_y'] == df['price']], 
                       [np.nan, 'match'], default='mismatch')
df = df.rename(columns={'price_y':'actual_price'})
print (df)
    code  price                date      time product  actual_price      flag
0  12023  71.23 2018-01-01 06:23:00  06:23:00      MS         71.23     match
1  12023  61.00 2018-01-01 07:56:00  07:56:00      HS         61.00     match
2  12023  71.23 2018-01-01 08:34:00  08:34:00      MS         71.23     match
3  12023  71.30 2018-01-01 06:03:00  06:03:00      MS         71.23  mismatch
4  12023  61.00 2018-01-01 11:43:00  11:43:00      HS         61.00     match
5  12023  71.23 2018-01-01 10:11:00  10:11:00      MS         71.23     match
6  12023  71.23 2018-01-01 04:23:00  04:23:00      MS           NaN       nan
7  12023  72.23 2018-01-02 10:11:00  10:11:00      MS         72.23     match
8  12023  72.23 2018-01-02 04:23:00  04:23:00      MS         71.23  mismatch