1

In this dataset, I want to find out (element-wise) whether date happens after the end of the period given in week.

    import pandas as pd
    df = pd.DataFrame({
    "week": pd.period_range('2018-03-01', '2018-05-01', freq='W'),
    "date": pd.date_range('2018-04-09', periods=10, freq='D')
    })

Expected Output:

                    week       date    out
0  2018-02-26/2018-03-04 2018-04-09  True
1  2018-03-05/2018-03-11 2018-04-10  True
2  2018-03-12/2018-03-18 2018-04-11  True
3  2018-03-19/2018-03-25 2018-04-12  True
4  2018-03-26/2018-04-01 2018-04-13  True
5  2018-04-02/2018-04-08 2018-04-14   True
6  2018-04-09/2018-04-15 2018-04-15   False
7  2018-04-16/2018-04-22 2018-04-16   False
8  2018-04-23/2018-04-29 2018-04-17   False
9  2018-04-30/2018-05-06 2018-04-18   False

I tried end_time, but this does not work with a Series of Periods.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
E. Sommer
  • 710
  • 1
  • 7
  • 28

1 Answers1

2

Use Series.to_timestamp with .dt for not working with DatetimeIndex and parameter how='e':

df['out'] = df['week'].dt.to_timestamp(how='e') < df['date']
print (df)
                    week       date    out
0  2018-02-26/2018-03-04 2018-04-09   True
1  2018-03-05/2018-03-11 2018-04-10   True
2  2018-03-12/2018-03-18 2018-04-11   True
3  2018-03-19/2018-03-25 2018-04-12   True
4  2018-03-26/2018-04-01 2018-04-13   True
5  2018-04-02/2018-04-08 2018-04-14   True
6  2018-04-09/2018-04-15 2018-04-15  False
7  2018-04-16/2018-04-22 2018-04-16  False
8  2018-04-23/2018-04-29 2018-04-17  False
9  2018-04-30/2018-05-06 2018-04-18  False
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252