将变量传递给lambda函数

时间:2019-01-28 22:49:38

标签: python datetime lambda timedelta

我有以下数据框,如下所示:

print(df_example.to_dict())
{
    "slot_id": {
        0: 1,
        1: 2,
        2: 3,
        3: 4,
        4: 5
    },
    "class": {
        0: "A",
        1: "B",
        2: "C",
        3: "D",
        4: "E"
    },
    "day": {
        0: "Monday",
        1: "Tuesday",
        2: "Wednesday",
        3: "Thursday",
        4: "Saturday"
    },
    "base_date": {
        0: Timestamp("2019-01-21 00: 00: 00"),
        1: Timestamp("2019-01-22 00: 00: 00"),
        2: Timestamp("2019-01-23 00: 00: 00"),
        3: Timestamp("2019-01-24 00: 00: 00"),
        4: Timestamp("2019-01-26 00: 00: 00")
    },
    "next_date": {
        0: nan,
        1: nan,
        2: nan,
        3: nan,
        4: nan
    },
    "base_date_weekday": {
        0: 0,
        1: 1,
        2: 2,
        3: 3,
        4: 5
    }
}

df_example.head

对于列next_date,我想使用下面的lambda函数在今天的日期中找到列day中星期几(星期一,星期二等)的下一个日期。 [found here]

df_example['base_date'] = pd.to_datetime(df_example['base_date'].astype(str), format='%d%m%Y')
df_example['base_date_weekday'] = df_example['base_date'].dt.dayofweek
# Lambda function to find the next date that corresponds to a particular day of the week
onDay = lambda date, day: date + datetime.timedelta(days=(day-date.weekday()+7)%7)

lambda函数使用一个简单的整数。

df_example['next_date'] = onDay(date=datetime.date.today(), day=2)

但是如何将列day传递给lambda的day参数,以便它逐行进行计算?没有下列作品:

df_example['next_date'] = onDay(date=datetime.date.today(), day=df_example['base_date_weekday'])
df_example['next_date'] = onDay(date=datetime.date.today(), day='base_date_weekday')

我试图使用一个函数和df.apply都没有成功。

def next_weekday(weekday):
    todaydate = datetime.date.today()
    days_ahead = weekday - todaydate.weekday()
    if days_ahead <= 0: # Target day already happened this week
        days_ahead += 7
    return todaydate + datetime.timedelta(days_ahead)

df_example.apply(lambda base_date_weekday: next_weekday(base_date_weekday),axis=1)

有什么主意我怎么能完成这个简单的任务?

1 个答案:

答案 0 :(得分:0)

我设法通过定义一个函数并将其传递给for循环(!),从今天开始获取下一个工作日。我敢肯定,还有更多的Python方式可以做到这一点,对此我将不胜感激。

def next_weekday(weekday):
    todaydate = datetime.date.today()
    days_ahead = weekday - todaydate.weekday()
    if days_ahead <= 0: # Target day already happened this week
        days_ahead += 7
    return todaydate + datetime.timedelta(days_ahead)

for i in range(df_example.shape[0]):
    df_example.loc[i,'next_date'] = next_weekday(weekday=int(df_example.loc[i,'base_date_weekday']))
    print(df_example)