获得 pandas 系列赛一周的第一天

2022-05-12 00:00:00 python pandas datetime series

问题描述

我有以下df:

import pandas as pd
from datetime import datetime, timedelta

df = pd.DataFrame([
        ["A", "2018-08-03"],
        ["B", "2018-08-20"]
])
df.columns = ["Item", "Date"]

我想为我的df的每一行获得一周的第一天。我试图这样做:

df['Date'] =  pd.to_datetime(df['Date'], format='%Y-%m-%d')
df["Day_of_Week"] = df.Date.dt.weekday

df["First_day_of_the_week"] = df.Date - timedelta(days=df.Day_of_Week)

但我收到错误消息:

TypeError: unsupported type for timedelta days component: Series

如何才能获得系列每周的第一天? 我的预期结果是:

  • "A","2018-08-03","2018-07-30"
  • "B","2018-08-20","2018-08-20"

解决方案

遗憾的是,timedelta不支持矢量化形式,因此我选择apply

df["First_day_of_the_week"] = df.apply(lambda x: x['Date'] - timedelta(days=x['Day_of_Week']), axis=1)

编辑

timedelta不支持矢量化参数,但可以乘以向量:)

df["First_day_of_the_week"] = df.Date - df.Day_of_Week * timedelta(days=1)

相关文章