How to drop datapoints from index

97 views Asked by At

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?

2

There are 2 answers

0
jezrael On BEST ANSWER

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
0
Scott Boston On

Assuming that you really want to start at '2017-07-11' instead of '2017-11-07'(which is after your enddate of '2017-07-23'), you can use Partial String Indexing:

SETUP

df = pd.DataFrame(index = pd.date_range('2017-05-07 00:00:00+00:00','2017-07-27 23:50:00+00:00', freq='10T'))
print(df.index)

DatetimeIndex(['2017-05-07 00:00:00+00:00', '2017-05-07 00:10:00+00:00',
               '2017-05-07 00:20:00+00:00', '2017-05-07 00:30:00+00:00',
               '2017-05-07 00:40:00+00:00', '2017-05-07 00:50:00+00:00',
               '2017-05-07 01:00:00+00:00', '2017-05-07 01:10:00+00:00',
               '2017-05-07 01:20:00+00:00', '2017-05-07 01:30:00+00:00',
               ...
               '2017-07-27 22:20:00+00:00', '2017-07-27 22:30:00+00:00',
               '2017-07-27 22:40:00+00:00', '2017-07-27 22:50:00+00:00',
               '2017-07-27 23:00:00+00:00', '2017-07-27 23:10:00+00:00',
               '2017-07-27 23:20:00+00:00', '2017-07-27 23:30:00+00:00',
               '2017-07-27 23:40:00+00:00', '2017-07-27 23:50:00+00:00'],
              dtype='datetime64[ns, UTC]', length=11808, freq='10T')

Now, use partial string indexing with slicing:

df1 = df['2017-07-11':'2017-07-22 23:50:00']
print(df_1.index)

Output: a smaller dataframe with time before 2017-07-11 and after 2017-07-22 23:50 dropped:

DatetimeIndex(['2017-07-11 00:00:00+00:00', '2017-07-11 00:10:00+00:00',
               '2017-07-11 00:20:00+00:00', '2017-07-11 00:30:00+00:00',
               '2017-07-11 00:40:00+00:00', '2017-07-11 00:50:00+00:00',
               '2017-07-11 01:00:00+00:00', '2017-07-11 01:10:00+00:00',
               '2017-07-11 01:20:00+00:00', '2017-07-11 01:30:00+00:00',
               ...
               '2017-07-22 22:20:00+00:00', '2017-07-22 22:30:00+00:00',
               '2017-07-22 22:40:00+00:00', '2017-07-22 22:50:00+00:00',
               '2017-07-22 23:00:00+00:00', '2017-07-22 23:10:00+00:00',
               '2017-07-22 23:20:00+00:00', '2017-07-22 23:30:00+00:00',
               '2017-07-22 23:40:00+00:00', '2017-07-22 23:50:00+00:00'],
              dtype='datetime64[ns, UTC]', length=1728, freq='10T')