How to assemble complex periods in Pandas

时间:2018-03-08 22:13:27

标签: python pandas

So in pandas, we can generate a simple monthly period index via the following

pi = pandas.period_range("2018-01", periods=10, freq='M')

Output:

PeriodIndex(['2018-01', '2018-02', '2018-03', '2018-04', '2018-05', '2018-06',
         '2018-07', '2018-08', '2018-09', '2018-10'],
        dtype='period[M]', freq='M')

We can also generate a set of period dates by applying the following method.

pi.to_timestamp()

Output:

DatetimeIndex(['2018-01-01', '2018-02-01', '2018-03-01', '2018-04-01',
           '2018-05-01', '2018-06-01', '2018-07-01', '2018-08-01',
           '2018-09-01', '2018-10-01'],
          dtype='datetime64[ns]', freq='MS')

Simple periodic sets of dates can be assembled using the following above. However, how can more complex sets of periods be assembled using pandas? Let's say that we wanted to define a period of the following...

For each month, get the Friday of the 3rd week within the month

I'm looking to get insight into how to define composite periods within the Pandas API. I'm getting rather confused with the role of the pandas Period and PeriodIndex classes within all of this and how concrete Datetimes/Timestamps are generated from instances of both of these classes.

1 个答案:

答案 0 :(得分:1)

例如,

import pandas as pd

dt = pd.date_range("2001-01-01", "2002-01-01")
print(dt)

filtered_dt = dt[
    (dt.dayofweek == 0) &
    (dt.month % 2 == 0)
]

print(filtered_dt)

给出,

DatetimeIndex(['2001-01-01', '2001-01-02', '2001-01-03', '2001-01-04',
               '2001-01-05', '2001-01-06', '2001-01-07', '2001-01-08',
               '2001-01-09', '2001-01-10',
               ...
               '2001-12-23', '2001-12-24', '2001-12-25', '2001-12-26',
               '2001-12-27', '2001-12-28', '2001-12-29', '2001-12-30',
               '2001-12-31', '2002-01-01'],
              dtype='datetime64[ns]', length=366, freq='D')
DatetimeIndex(['2001-02-05', '2001-02-12', '2001-02-19', '2001-02-26',
               '2001-04-02', '2001-04-09', '2001-04-16', '2001-04-23',
               '2001-04-30', '2001-06-04', '2001-06-11', '2001-06-18',
               '2001-06-25', '2001-08-06', '2001-08-13', '2001-08-20',
               '2001-08-27', '2001-10-01', '2001-10-08', '2001-10-15',
               '2001-10-22', '2001-10-29', '2001-12-03', '2001-12-10',
               '2001-12-17', '2001-12-24', '2001-12-31'],
              dtype='datetime64[ns]', freq=None)