Reputation: 103
I have the following datetime index and dataframe object:
dti = pd.DatetimeIndex(['2019-06-02', '2019-06-16', '2019-07-07', '2019-07-21', '2019-08-04'], name='Date')
df = pd.DataFrame(columns=['Open', 'High', 'Low', 'Close'], index=dti,
data={
'Open': [1.27223, 1.27340, 1.26976, 1.25736, 1.23806],
'High': [1.27223, 1.27340, 1.26976, 1.25736, 1.23806],
'Low': [1.27223, 1.27340, 1.26976, 1.25736, 1.23806],
'Close': [1.27223, 1.27340, 1.26976, 1.25736, 1.23806]
})
It is a weekly based data, each bar is a weekly bar on a finance market. I would like to select daily bars for each df row, for example
2019-06-16: [2019-06-10, 2019-06-11, ..., 2019-06-16]
2019-07-07: [2019-07-01, 2019-07-02, ..., 2019-07-07]
...
at the end it should be a list of dataframes. All data from daily bars exists.
The question for me is how to find a start date of weekly bar, then I could use slicing , like [from_date:to_date]
And second issue is that it can be not only weekly bars, it could be any other period: monthly, quarterly, ...
When I had ordered continuous series of dates, I've used Boolean operations, but here this approach doesn't work:
# get components values range
comp_res = comp[(comp.index > _from) & (comp.index <= _to)]
Upvotes: 0
Views: 112
Reputation: 29742
Use pandas.Series.to_period
:
pd.Series(df.index).dt.to_period('w').dt.start_time
Output:
0 2019-05-27
1 2019-06-10
2 2019-07-01
3 2019-07-15
4 2019-07-29
Name: Date, dtype: datetime64[ns]
Upvotes: 2