Reputation: 1754
date_idx
DatetimeIndex(['2021-05-14', '2021-05-17', '2021-05-18', '2021-05-19',
'2021-05-20', '2021-05-21', '2021-05-24', '2021-05-25',
'2021-05-26', '2021-05-27', '2021-05-28', '2021-05-31',
'2021-06-01', '2021-06-02', '2021-06-03', '2021-06-04',
'2021-06-07', '2021-06-08', '2021-06-09', '2021-06-10',
'2021-06-11', '2021-06-15', '2021-06-16', '2021-06-17',
'2021-06-18', '2021-06-21', '2021-06-22', '2021-06-23',
'2021-06-24', '2021-06-25', '2021-06-28', '2021-06-29',
'2021-06-30', '2021-07-01', '2021-07-02', '2021-07-05',
'2021-07-06', '2021-07-07', '2021-07-08', '2021-07-09',
'2021-07-12', '2021-07-13', '2021-07-14', '2021-07-15',
'2021-07-16', '2021-07-19', '2021-07-20', '2021-07-21',
'2021-07-22', '2021-07-23'],
dtype='datetime64[ns]', name='date', freq=None)
Goal
Try
date_idx- date_idx.weekday * np.timedelta64(1, 'D')
'2021-06-15'
will be '2021-06-14'
but it should be '2021-06-15'
because '2021-06-14'
not in date_idx
.Upvotes: 0
Views: 57
Reputation: 22493
IIUC you can groupby
with freq="W"
and then transform
first:
date_idx.to_frame(False).groupby(pd.Grouper(freq="W", key="date"))["date"].transform("first")
0 2021-05-14
1 2021-05-17
2 2021-05-17
3 2021-05-17
4 2021-05-17
5 2021-05-17
6 2021-05-24
7 2021-05-24
8 2021-05-24
9 2021-05-24
10 2021-05-24
11 2021-05-31
12 2021-05-31
13 2021-05-31
14 2021-05-31
15 2021-05-31
16 2021-06-07
17 2021-06-07
18 2021-06-07
19 2021-06-07
20 2021-06-07
21 2021-06-15
22 2021-06-15
23 2021-06-15
24 2021-06-15
25 2021-06-21
26 2021-06-21
27 2021-06-21
28 2021-06-21
29 2021-06-21
30 2021-06-28
31 2021-06-28
32 2021-06-28
33 2021-06-28
34 2021-06-28
35 2021-07-05
36 2021-07-05
37 2021-07-05
38 2021-07-05
39 2021-07-05
40 2021-07-12
41 2021-07-12
42 2021-07-12
43 2021-07-12
44 2021-07-12
45 2021-07-19
46 2021-07-19
47 2021-07-19
48 2021-07-19
49 2021-07-19
Name: date, dtype: datetime64[ns]
Upvotes: 1