1

I am trying to calculate the difference between two index values for a dataframe (number of days between first and last index values in dataframe). I have the following error (below). I was wondering if there is a workaround for this (it seems the below code was working OK for a small dataframe..)? Thanks

type(data1.index)
pandas.core.indexes.base.Index


data1.index = pd.to_datetime(data1.index)
type(data1.index)
pandas.core.indexes.datetimes.DatetimeIndex


d1 = data1.index[-1]
d1
Timestamp('2022-11-02 00:00:00')


d3 = data1.index[1]
d3
Timestamp('2021-10-03 00:00:00')


(d1-d3).days[0]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
C:Temp/ipykernel_3208/1081380695.py in <module>
----> 1 (d1-d3).days[0]

TypeError: 'int' object is not subscriptable

1 Answer 1

1

Use only Timedelta.days, because subtract scalars to scalar Timedelta:

out = (d1-d3).days
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.