Question
- I want the result of Pandas
.resample() method to have 2 levels of index. As you can check it out on the example below, the resampling of good_example_df produces the 2 level of index.
- When I changed the 2nd element of
index_list from datetime(2020,1,2,12,39,59) to datetime(2020,1,3,12,39,59), the .resample() method gives me only 1 level of index. The example is given with bad_example_df. It is something that I do not want to take place.
- How can I make the result of
.resample() method to have always the 2 level of index, as the good_example_df does?
- The reason I am asking this question is that I want the result of the resample method to be consistent. I don't want the result of the method to differ depending on the input.
Code Example
import pandas as pd
from datetime import datetime, time
index_list = [datetime(2020,1,2,2,43,59), datetime(2020,1,2,12,39,59),datetime(2020,1,3,21,42,59),
datetime(2020,1,4,2,53,59), datetime(2020,1,4,19,17,59)]
data_dict = {'return': [1.003, 1.02, 1.06, 1.02, 1.03],
'stock' : ['AMZN', 'APPL', 'NVDA', 'MSFT', 'AMZN']}
good_example_df = pd.DataFrame(data = data_dict, index = index_list)
good_example_df
>> return stock
2020-01-02 2:43:59 1.003 AMZN
2020-01-02 12:39:59 1.020 APPL
2020-01-03 21:42:59 1.060 NVDA
2020-01-04 02:53:59 1.020 MSFT
2020-01-04 19:17:59 1.030 AMZN
good_example_df.resample("D").apply(lambda x: x[:2])
>> return stock
2020-01-02 2020-01-02 02:43:59 1.003 AMZN
2020-01-02 12:39:59 1.020 APPL
2020-01-03 2020-01-03 21:42:59 1.060 NVDA
2020-01-04 2020-01-04 02:53:59 1.020 MSFT
2020-01-04 19:17:59 1.030 AMZN
new_index_list = [datetime(2020,1,2,2,43,59), datetime(2020,1,3,12,39,59),datetime(2020,1,3,21,42,59),
datetime(2020,1,4,2,53,59), datetime(2020,1,4,19,17,59)]
bad_example_df = pd.DataFrame(data = data_dict, index = new_index_list)
bad_example_df
>> return stock
2020-01-02 2:43:59 1.003 AMZN
2020-01-03 12:39:59 1.020 APPL
2020-01-03 21:42:59 1.060 NVDA
2020-01-04 02:53:59 1.020 MSFT
2020-01-04 19:17:59 1.030 AMZN
bad_example_df.resample("D").apply(lambda x: x[:2])
>>> return stock
2020-01-02 1.003 AMZN
2020-01-03 [1.02, 1.06] [APPL, NVDA]
2020-01-04 [1.02, 1.03] [MSFT, AMZN]