Search code examples
pythonpandasindexingreindex

How to drop datapoints from index


I am new to Python and I have a dataset S2 including dates. When I use the command:

available_datapoints = S2.index, 

then

print(available_datapoints) 

yields:

<class 'pandas.tseries.index.DatetimeIndex'>
[2017-05-07 00:00:00+00:00, ..., 2017-07-27 23:50:00+00:00]
Length: 11808, Freq: 10T, Timezone: UTC stop

However Instead of 2017-05-07 00:00:00+00:00, I want to start 2017-11-07 00:00:00+00:00 and instead of 2017-07-27 23:50:00+00:00, I want to stop 2017-07-22 23:50:00+00:00.

Anyone knows how I change this?


Solution

  • I think you can use DataFrame.truncate:

    #Sample data
    S2 = pd.DataFrame({'a': range(11808)}, 
                       index=pd.date_range(start='2017-05-07',periods=11808, freq='10T'))
    print (S2.head())
                         a
    2017-05-07 00:00:00  0
    2017-05-07 00:10:00  1
    2017-05-07 00:20:00  2
    2017-05-07 00:30:00  3
    2017-05-07 00:40:00  4
    
    print (S2.tail())
                             a
    2017-07-27 23:10:00  11803
    2017-07-27 23:20:00  11804
    2017-07-27 23:30:00  11805
    2017-07-27 23:40:00  11806
    2017-07-27 23:50:00  11807
    

    S2 = S2.truncate(before='2017-07-11', after='2017-07-22 23:50:00')
    print (S2.head())
                            a
    2017-07-11 00:00:00  9360
    2017-07-11 00:10:00  9361
    2017-07-11 00:20:00  9362
    2017-07-11 00:30:00  9363
    2017-07-11 00:40:00  9364
    
    print (S2.tail())
                             a
    2017-07-22 23:10:00  11083
    2017-07-22 23:20:00  11084
    2017-07-22 23:30:00  11085
    2017-07-22 23:40:00  11086
    2017-07-22 23:50:00  11087