I have a dataframe
| company_name | founded_on |
|---|---|
| a | 2004-01-01 00:00:00 |
| b | 2013-01-01 00:00:00 |
| c | 2008-01-01 00:00:00 |
| d | 1997-01-01 00:00:00 |
today = pd.Timestamp.now()
df["founded_on"]=pd.to_datetime(df["founded_on"])
df["Time_Since_founded_on"] = (today - df["founded_on"]).dt.days // 30
OverflowError: Overflow in int64 addition
I want to know how many months each companies have been in operation.
Use monthly period operations, converting using
to_period('M'):Output:
Note that your original approach (using days) would give incorrect values for large periods since months are not exactly equal to 30D:
A better (but not perfect) approximation would be to divide by
365.25/12.handling invalid/missing dates
If dates are invalid/missing, you need to adapt the code to use
errors='coerce'anddropnabefore extracting the number of months:Output: