I have a datetimeindex with 289 datetimestamps and I want to remove the 289th datetime stamp. Could you please let me know how to remove the last datetime stamp from the datetime index in pandas
1 Answer
s is a DatetimeIndex covering 289 days starting from Jan 1, 2019:
s = pd.date_range('2019-01-01', periods=289)
DatetimeIndex(['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04',
'2019-01-05', '2019-01-06', '2019-01-07', '2019-01-08',
'2019-01-09', '2019-01-10',
...
'2019-10-07', '2019-10-08', '2019-10-09', '2019-10-10',
'2019-10-11', '2019-10-12', '2019-10-13', '2019-10-14',
'2019-10-15', '2019-10-16'],
dtype='datetime64[ns]', length=289, freq='D')
Use array slicing to exclude the last one:
s[:-1]
DatetimeIndex(['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04',
'2019-01-05', '2019-01-06', '2019-01-07', '2019-01-08',
'2019-01-09', '2019-01-10',
...
'2019-10-06', '2019-10-07', '2019-10-08', '2019-10-09',
'2019-10-10', '2019-10-11', '2019-10-12', '2019-10-13',
'2019-10-14', '2019-10-15'],
dtype='datetime64[ns]', length=288, freq='D')
datetime index. You can just apply:df.iloc[:-1]and it will remove the last row.