Get the first day of the week for a Pandas series

Question:

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”
Asked By: Charles R

||

Answers:

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)
Answered By: kosnik

Leave out your ‘Day of week” calculation and do this.

df["First_day_of_the_week"] = df['Date'].apply(lambda x: (x - timedelta(days=x.dayofweek)))
print(df)

giving

  Item       Date First_day_of_the_week
0    A 2018-08-03            2018-07-30
1    B 2018-08-20            2018-08-20
Answered By: Spinor8

A vectorised solution is possible with NumPy:

df['First_day'] = df['Date'] - df['Date'].dt.weekday * np.timedelta64(1, 'D')

print(df)

  Item       Date  First_day
0    A 2018-08-03 2018-07-30
1    B 2018-08-20 2018-08-20
Answered By: jpp

You can stay in Pandas and use its DateOffset objects:

>>> from pandas.tseries.offsets import Week

>>> df.Date.where(df.Date.dt.weekday == 0, df.Date - Week(weekday=0))
0   2018-07-30
1   2018-08-20
Name: Date, dtype: datetime64[ns]

The trick being that you need to not do the subtraction where the weekday is already Monday (weekday == 0). This says, “in cases where weekday is already zero, do nothing; else, return Monday of that week.”

Answered By: Brad Solomon

pandas version

df = pd.DataFrame({
    'Item': ['A', 'B'],
    'Date': ['2018-08-03', '2018-08-20']
})

df['Date'] = pd.to_datetime(df.Date) #Use pd.Timestamp
df.Date - pd.TimedeltaIndex(df.Date.dt.dayofweek,unit='d') 

Output:

0   2018-07-30
1   2018-08-20
dtype: datetime64[ns]

Docs on used functions: pd.TimedeltaIndex, pd.to_datetime

Working with date and time: Time Series / Date functionality

Answered By: ilia timofeev

Here’s a solution that doesn’t require timedelta or lambda functions with subtractions:

df['Date'].dt.to_period('W').dt.to_timestamp()
Answered By: Fernando Wittmann
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.