Home > Software design >  Pandas: Filtered property behaves like unfiltered
Pandas: Filtered property behaves like unfiltered

Time:04-19

I have DT, which is a datetime64 Series:

0       2019-12-12 18:43:00
1       2019-03-22 18:30:00
2                       NaT
3       2019-04-17 02:00:00
4       2009-03-15 18:00:00
5       2019-04-02 20:25:00
6       2019-05-01 11:00:00
7       2019-04-10 17:00:00
8       1973-07-14 22:00:00
9       2019-06-06 19:00:00
10      2019-06-18 21:00:00
11      2019-06-12 22:00:00
12      2019-06-11 22:00:00
13      2018-06-15 01:00:00
14      1999-08-15 02:30:00
                ...        
88110   2019-10-01 22:00:00
88111   2019-10-01 22:45:00
88112   2019-10-02 01:00:00
88113   2019-10-02 03:26:00
88114   2019-10-02 03:26:00
88115   2019-10-02 05:33:00
88116   2019-10-02 06:35:00
88117   2019-10-02 12:00:00
88118   2019-10-02 19:00:00
88119   2019-10-02 19:15:00
88120   2019-10-02 20:00:00
88121   2019-10-02 20:00:00
88122   2019-10-02 20:03:00
88123   2019-10-02 22:00:00
88124   2019-10-02 22:00:00
Name: date_time, Length: 88125, dtype: datetime64[ns]

and a piece of code:

DT[DT.between("2019-12-05", "2019-12-08") & DT.dt.weekday == 1].dt.weekday.value_counts()

which yields:

5    27
3    23
4    19
Name: date_time, dtype: int64

which includes 3, 4 and 5 days but not a single requested day 1!

So, when I code just:

DT[DT.between("2019-12-05", "2019-12-08")].dt.weekday

it yields:

3821     3
87138    3
87139    3
87140    3
87141    3
        ..
87328    5
87329    5
87330    5
87331    5
87332    5

which is logical because we have 3 days interval, which corresponds to 3 week days. And yes, we do not have week day 1 at all in our days range! So why does this & DT.dt.weekday == 1 filter not work?

Thank you a lot for your time!

UPDATE

When I try to use any other filter like & DT.dt.weekday == 2, & DT.dt.weekday == 3 etc., I get an empty Series as a result of the filtering like this:

DT[DT.between("2019-12-05", "2019-12-08") & DT.dt.weekday == 4]

Moreover, DT.dt.weekday == 1 returns normal True/False list!

Maybe, we cannot filter by dt.(...) parameters?

CodePudding user response:

Turns out that this:

DT[DT.between("2019-12-05", "2019-12-08") & DT.dt.weekday == 1]

is performed as this:

DT[ (DT.between("2019-12-05", "2019-12-08") & DT.dt.weekday)   ==   1 ]

which is why DT.dt.weekday the filter returned True for each day between 2019-12-05 and -08 because & DT.dt.weekday never really influenced as it was 3 to 5 for all the mentioned days range.

So, when I coded it like this:

DT[ (DT.between("2019-12-05", "2019-12-08")) & (DT.dt.weekday == 1) ]

everything worked out as was expected, i.e. nothing was chosen. But this, on the other hand:

DT[ (DT.between("2019-12-05", "2019-12-08")) & (DT.dt.weekday == 3) ]

yielded resulted in a few lines corresponding to day 3.

So, once parentheses are correctly put to separate A and B statements in A & B filtering expression, everything works as designed!

Thank you all for your time anyway! =)

  • Related