Search code examples
pythonpandasdatetimeseries

Get the first day of the week for a Pandas series


I have the following 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"]

I want to get the first day of the week for every line of my df. I tried to do this :

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)

But I got that error message :

TypeError: unsupported type for timedelta days component: Series

How can I get the first day of the week for a Series ? My expected result is that :

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

Solution

  • Unfortunately timedelta doesn't support a vectorized form so I would go for an apply

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

    EDIT

    timedelta doesn't support vectorized arguments but can be multiplied by a vector :)

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