根据当前月份计算的列

时间:2018-08-08 14:02:11

标签: python python-3.x pandas numpy datetime

我有一个数据框,要在其中添加基于两个列“ DATE”和“ Delivery_Date”的计算列“ Status”(数据类型:datetime64 [ns])

Eg. if(DATE & Delivery_Date contains current month) then (status = yes)
    else (status = no)

   DATE        ||  Delivery_Date    ||      Status
===================================================
01-03-2018     ||   01-08-2018      ||        No
---------------------------------------------------
01-08-2018     ||   30-08-2018      ||        yes
---------------------------------------------------
10-08-2018     ||   28-08-2018      ||        yes
---------------------------------------------------
01-05-2018     ||   31-05-2018      ||        No
---------------------------------------------------

我使用了一种方法(如下所示)来获取当前月份:

from datetime import datetime

currentMonth = datetime.now().month
currentYear = datetime.now().year

我尝试过:

conditions = [(df_new_status['DATE'] == datetime.now().month) & (df_new_status['Delivery_Date'] == datetime.now().month)]
choices = ['Yes']
df_new_status['status'] = np.select(conditions, choices, default='No')

(TypeError: invalid type comparison)

4 个答案:

答案 0 :(得分:3)

您可以使用Pandas可用的方法来代替使用datetime模块。由于您的列已经为datetime格式,因此尤其如此。您可以将pd.Series.dt.monthnumpy.where一起使用来满足单个条件:

this_month = pd.to_datetime('today').month  # e.g. 8 for August

m1 = df['DATE'].dt.month == this_month
m2 = df['Delivery_Date'].dt.month == this_month

df['status'] = np.where(m1 & m2, 'Yes', 'No')

答案 1 :(得分:0)

尝试一下

df['Status'] = 'No'
df.loc[(df_new_status['DATE'].dt.month == datetime.now().month) & (df_new_status['Delivery_Date'].dt.month == datetime.now().month), 'Status'] = 'Yes'

我认为您遇到了该错误,因为您比较了一个序列(df ['DATE'])和一个int currentMonth

答案 2 :(得分:0)

尝试一下:

import numpy as np
df['Status'] = np.where(pd.DatetimeIndex(df['Date']).month == pd.DatetimeIndex(df['Delivery_Date']).month, 'Yes', 'No')

输出:

         Date Delivery_Date  Status
0  01-03-2018    01-08-2018     No
1  01-08-2018    30-08-2018     Yes
2  10-08-2018    28-08-2018     Yes
3  01-05-2018    31-05-2018     Yes

基于JPP的理解,我还提供了另一种解决方案:

df['Status'] = np.where((pd.DatetimeIndex(df['Date']).month == pd.to_datetime('today').month) & (pd.to_datetime('today').month == pd.DatetimeIndex(df['Delivery_Date']).month), 'Yes', 'No')

输出:

     Date     Delivery_Date  Status
0  01-03-2018    01-08-2018     No
1  01-08-2018    30-08-2018     Yes
2  10-08-2018    28-08-2018     Yes
3  01-05-2018    31-05-2018     No

答案 3 :(得分:0)

尝试一下

import pandas as pd
from datetime import datetime
test = pd.DataFrame({'DATE': ['01-03-2018', '01-08-2018'], 'Del_Date': ['01-08-2018', '30-08-2018']})
test['DATE'] =test['DATE'].map(lambda x: datetime.strptime(x, '%d-%m-%Y'))
test['Del_Date'] =test['Del_Date'].map(lambda x: datetime.strptime(x, '%d-%m-%Y'))
test
    DATE        Del_Date
0   2018-03-01  2018-08-01
1   2018-08-01  2018-08-30

test['Status'] = test.apply(lambda x: x['DATE'].month == x['Del_Date'].month, axis=1)
test
    DATE        Del_Date    Status
0   2018-03-01  2018-08-01  False
1   2018-08-01  2018-08-30  True

这将为您提供True或False,而不是Yes和No。您可以稍后根据需要将其重命名。

希望这会有所帮助。