Date and time calculations using Numpy timedelta64.
Different units are used with timedelta64 for calculations, the list of units are given at the end of this tutorial.
Let us create DataFrame with two datetime columns to calculate the difference.
import pandas as pd
my_dict={'NAME':['Ravi','Raju','Alex'],
'dt_start':['1/1/2020','2/1/2020','5/1/2020'],
'dt_end':['6/15/2022','7/22/2022','11/15/2023']
}
my_data = pd.DataFrame(data=my_dict)
my_data['dt_start'] = pd.to_datetime(my_data['dt_start'])
my_data['dt_end'] = pd.to_datetime(my_data['dt_end'])
print(my_data)
Output
NAME dt_start dt_end
0 Ravi 2020-01-01 2022-06-15
1 Raju 2020-02-01 2022-07-22
2 Alex 2020-05-01 2023-11-15
my_data['diff_days']=my_data['dt_end']-my_data['dt_start']
print(my_data)
NAME dt_start dt_end diff_days
0 Ravi 2020-01-01 2022-06-15 896 days
1 Raju 2020-02-01 2022-07-22 902 days
2 Alex 2020-05-01 2023-11-15 1293 days
references:
https://www.plus2net.com/python/pandas-dt-timedelta64.php
No comments:
Post a Comment