Skip to content
Advertisement

Pandas Timedelta in months

How can I calculate the elapsed months using pandas? I have write the following, but this code is not elegant. Could you tell me a better way?

JavaScript

Advertisement

Answer

Update for pandas 0.24.0:

Since 0.24.0 has changed the api to return MonthEnd object from period subtraction, you could do some manual calculation as follows to get the whole month difference:

JavaScript

Wrap in a function:

JavaScript

Prior to pandas 0.24.0. You can round the date to Month with to_period() and then subtract the result:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement