Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter dates on multiindex dataframe

I'm looking for a way to filter a multiindex dataframe like the following by day of the week and/or selected dates. Let's say I need

  • a query to select only mondays;
  • another query in which I want to select all days except monday and friday;
  • a third query to select data present in an input list of dates, like select all dates in ['2015-05-14', '2015-05-21', '2015-05-22'];
  • and finally, a query combining selection based on day of week and a list of dates, like select all dates in ['2015-05-14', '2015-05-21', '2015-05-22'] and thursdays.

What's the way to do it?

                Col1        Col2     Col3    Col4
Date        Two 
2015-05-14  10   81.370003  6.11282  39.753  44.950001
            11   80.419998  6.03380  39.289  44.750000
            C3   80.879997  6.00746  41.249  44.360001
2015-05-19   3   80.629997  6.10465  41.047  40.980000
            S9   80.550003  6.14370  41.636  42.790001
2015-05-21  19   80.480003  6.16096  42.137  43.680000
2015-05-22  C3   80.540001  6.13916  42.179  43.490002
like image 674
Bella Avatar asked Jan 31 '23 02:01

Bella


1 Answers

If you have the Date as datetime type, you can just use dayofweek to get the day of week and query based on it.

Select only Mondays:

df[df.index.get_level_values('Date').dayofweek == 0]

Select days except Monday and Friday:

import numpy as np
df[np.in1d(df.index.get_level_values('Date').dayofweek, [1,2,3,5,6])]

#                    Col1      Col2   Col3       Col4
#      Date Two             
#2015-05-14 10  81.370003   6.11282 39.753  44.950001
#           11  80.419998   6.03380 39.289  44.750000
#           C3  80.879997   6.00746 41.249  44.360001
#2015-05-19 3   80.629997   6.10465 41.047  40.980000
#           S9  80.550003   6.14370 41.636  42.790001
#2015-05-21 19  80.480003   6.16096 42.137  43.680000
like image 196
Psidom Avatar answered Feb 02 '23 09:02

Psidom