Home > other >  How to select only the last n days every month in the given pd.DatetimeIndex
How to select only the last n days every month in the given pd.DatetimeIndex

Time:12-27

For example,

datetimeidx = pd.DatetimeIndex(
          ['1999-03-01', '1999-03-02', '1999-03-03', '1999-03-04',
           '1999-03-05', '1999-03-08', '1999-03-09', '1999-03-10',
           '1999-03-11', '1999-03-12', '2021-11-16', '2021-11-17', 
           '2021-11-18', '2021-11-19', '2021-11-22', '2021-11-23', 
           '2021-11-24', '2021-11-26', '2021-11-29', '2021-11-30'])

if n=3, what I want is:

datetimeidx = pd.DatetimeIndex(
          ['1999-03-10','1999-03-11', '1999-03-12', 
           '2021-11-26', '2021-11-29', '2021-11-30'])

The point is that I want to select only from the 'given' pd.DatetimeIndex

CodePudding user response:

You can use:

g = pd.Series(datetimeidx.year).astype(str)   '-'   pd.Series(datetimeidx.month).astype(str)
print(pd.DatetimeIndex(pd.Series(datetimeidx).sort_values().groupby(g).tail(3)))

CodePudding user response:

you could group by year and month, then use the pandas.Series.tail;

n = 3

pd.DatetimeIndex(datetimeidx
                  .to_series()
                  .groupby([datetimeidx.year, datetimeidx.month])
                  .tail(n))

DatetimeIndex(['1999-03-10', '1999-03-11', '1999-03-12', '2021-11-26',
               '2021-11-29', '2021-11-30'],
              dtype='datetime64[ns]', freq=None)

CodePudding user response:

First make sure your datetime index is sorted, then we do

idx = datetimeidx.to_series().groupby(datetimeidx.strftime('%Y-%m')).tail(3).index
DatetimeIndex(['1999-03-10', '1999-03-11', '1999-03-12', '2021-11-26',
               '2021-11-29', '2021-11-30'],
              dtype='datetime64[ns]', freq=None)
  • Related