Although the @knightofni and @Paul solutions work, I try to avoid applying apply in Pandas because it is usually quite slow compared to array based methods. To avoid this, we can change the method based on the days of the week and simply convert the day of the week to numpy timedelta64 [D] .
df['week_start'] = df['myday'] - df['myday'].dt.weekday.astype('timedelta64[D]')
Using my test data with 60,000 dates, I got the following two times, using the other two suggested answers and the cast method.
%timeit df.apply(lambda x: x['myday'] - datetime.timedelta(days=x['myday'].weekday()), axis=1) >>> 1 loop, best of 3: 7.43 s per loop %timeit df['myday'].dt.to_period('W').apply(lambda r: r.start_time) >>> 1 loop, best of 3: 2.38 s per loop %timeit df['myday'] - df['myday'].dt.weekday.astype('timedelta64[D]') >>> 100 loops, best of 3: 12.3 ms per loop
or almost 200 times faster on my dataset.
n8yoder
source share