1

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

Krish
  • 67
  • 5
  • 4
    Why do you mention specifically `datetime index`. You can just apply: `df.iloc[:-1]` and it will remove the last row. – Erfan Jul 26 '19 at 22:00
  • 2
    Possible duplicate of [How to remove an element from a list by index?](https://stackoverflow.com/questions/627435/how-to-remove-an-element-from-a-list-by-index) –  Jul 27 '19 at 00:41

1 Answers1

1

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')
Code Different
  • 90,614
  • 16
  • 144
  • 163