0

I have some df with dates as the index. I need to aggregate over unique pair of dates. Thus, basically, I need to choose only two dates in the df, coming from the itertools.combinations() function. Notice I don't need the range but I need to filter for the two exact dates. Here's my solution, but it doesn't work.

Dtest = ['2019-09-23', '2019-09-24']
for pair in itertools.combinations(Dtest, 2):
    print(pair)
    tframe = df[(df.iloc(pair[0]) | df.iloc(pair[1])) ]
Dervin Thunk
  • 19,515
  • 28
  • 127
  • 217
  • Use `loc` instead? `iloc` is position based indexing, `loc` is label based. – Erfan Nov 12 '19 at 00:01
  • But then again, I'm not sure if you can index your data with strings if you index is datetime. Can you add a small example dataset, so we can reproduce? Read [this](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – Erfan Nov 12 '19 at 00:03
  • Apparently `2019-02-23` is not in your index, what does `print('2019-09-23' in df.index)` give? – Erfan Nov 12 '19 at 00:05
  • Good, btw I suggest you use `df.index.isin(Dtest)` instead of the loop. See bottom example in the [docs](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Index.isin.html) – Erfan Nov 12 '19 at 00:15

1 Answers1

0

Ok, so, I found a solution. I don't think it's optimal, but it does work.

Dtest = ['2019-09-23', '2019-09-24']
for pair in itertools.combinations(Dtest, 2):
    tframe = pd.concat([df[pair[0]], df[pair[1]]])
Dervin Thunk
  • 19,515
  • 28
  • 127
  • 217