0

I want to mix two different datasets. One has datetime as index and the other one as a column. In the first dataset, given a begin_date, in the first dataset, I want to slice 5 days starting from begin_date and then combine two datasets.

     date     Mean
0  2017-05-01  10
1  2017-05-02  30
2  2017-05-03  40
3  2017-05-04  50
4  2017-05-05  60
5  2017-05-06  70
6  2017-05-07  51
7  2017-05-08  66
8  2017-05-09  23


                vol1    vol2
2017-05-01  31.983047  31.985035
2017-05-02  31.158368  31.158368
2017-05-03  30.414481  30.414481
2017-05-04  29.680437  29.680680
2017-05-05  29.277345  29.277099

my favorite our put for begin_date = 2017-05-01 is:

    date       mean        vol1    vol2
0 2017-05-01    10     31.983047  31.985035
1 2017-05-02    30     31.158368  31.158368
2 2017-05-03    40     30.414481  30.414481
3 2017-05-04    50     29.680437  29.680680
4 2017-05-05    60     29.277345  29.277099

I tried:

begin_date = '2017-05-01'
finish_date = parse(begin_date)+pd.Timedelta('5 days')
end_date = str(datetime.strftime(finish_date, '%Y-%m-%d'))
df['date'].loc[begin_date:end_date]

but it doesn't extract that period properly.

mk_sch
  • 1,060
  • 4
  • 16
  • 31

1 Answers1

1

So here are few things to remember:

  1. convert all dates to pandas datetime (same with begin_date)
  2. if you want to slice df by date, set it as index.

So slightly changing your code:

begin_date = pd.to_datetime('2017-05-01')

(df1.set_index('date')[begin_date: begin_date + pd.Timedelta("5 days")]
 .join(df2))

if you want to remove dates from index in new df just use .reset_index in the end.

Hope it helps!

Kacper Wolkowski
  • 1,517
  • 1
  • 16
  • 24