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
CodePudding user response:
Use only Timedelta.days
, because subtract scalars to scalar Timedelta
:
out = (d1-d3).days