How do I convert datetime.timedelta to integer months?

237 Views Asked by At

Hopefully this is a simple fix, I've searched around on here to find a similar question and can't find something that deals with this specific issue. If it does exist the answers flew right over my head...

I have a list of start and end dates and I want to find the difference between them, in months, and then use that value to calculate something else. Here are the start and end dates:

Start Date End Date
2021-05-01 2022-09-01
2022-10-30 2023-03-30
2022-10-30 2023-12-31
2022-05-01 2023-03-30
2021-05-02 2023-03-30
2022-10-30 2023-09-01
2022-10-01 2023-09-30

In the past I have done it like this:

(df1['End Date'] - df1['Start Date'])/np.timedelta64(1, 'M')

and indeed when I run it, it returns:

0    16.033183
1     4.961087
2    14.029036
3    10.940676
4    22.899854
5    10.053595
6    11.959178
dtype: float64

Now I would like to do this in a for loop because there are rows in my data that I don't want this calculation to apply to. When I run it in a loop I get this error:

`UFuncTypeError: ufunc 'true_divide' cannot use operands with types dtype('O') and dtype('<m8[M]')`

I have tried to break it down. If I run this

`print((df1['End Date'].iloc[2] - df1['Start Date'].iloc[2]))`

I get this,

`427 days, 0:00:00`

but if I run this,

`print((df1['End Date'].iloc[2] - df1['Start Date'].iloc[2])/np.timedelta64(1, 'M'))`

I get the same error as above.

So I can see why an error is appearing, I think, but I don't know what to do about it. I've tried converting the date difference like this,

`(df1['End Date'].iloc[2] - df1['Start Date'].iloc[2]).astype("timedelta64")`

but it errors saying,

`AttributeError: 'datetime.timedelta' object has no attribute 'astype'`

So yeah. Now I'm stuck. I suppose I could add a column with the date difference and then use that onward for what I need, but I'd like to understand what I'm missing here.

1

There are 1 best solutions below

0
user16164298 On

Could try this:

(df1['End Date'].dt.to_period('M') - df1['Start Date'].dt.to_period('M'))