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

2
  • 4
    Why do you mention specifically datetime index. You can just apply: df.iloc[:-1] and it will remove the last row. Commented Jul 26, 2019 at 22:00
  • 2
    Possible duplicate of How to remove an element from a list by index? Commented Jul 27, 2019 at 0:41

1 Answer 1

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')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.