Skip to content Skip to sidebar Skip to footer

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 =

Solution 1:

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

Solution 2:

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)

Solution 3:

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

Solution 4:

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."


Solution 5:

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


Post a Comment for "Get The First Day Of The Week For A Pandas Series"