Calculate Time Difference Between Two Pandas Columns in Hours and Minutes
Pandas timestamp differences returns a datetime.timedelta object. This can easily be converted into hours by using the *as_type* method, like so
import pandas
df = pandas.DataFrame(columns=['to','fr','ans'])
df.to = [pandas.Timestamp('2014-01-24 13:03:12.050000'), pandas.Timestamp('2014-01-27 11:57:18.240000'), pandas.Timestamp('2014-01-23 10:07:47.660000')]
df.fr = [pandas.Timestamp('2014-01-26 23:41:21.870000'), pandas.Timestamp('2014-01-27 15:38:22.540000'), pandas.Timestamp('2014-01-23 18:50:41.420000')]
(df.fr-df.to).astype('timedelta64[h]')
to yield,
0 58
1 3
2 8
dtype: float64
How to calculate time difference in minutes from 2 data frame columns in Pandas
Use total_seconds
to get the time duration in seconds, and then divide by 60 to convert it to minutes
df['TIME_TO_REPORT']= (df['TEST_TIME'] - df['RECEIPT_DATE_TIME']).dt.total_seconds() / 60
Python : time difference between two columns in hours
The sample data doesn't have the time difference greater than 24hrs
In [26]: df = pd.read_csv("a.csv", parse_dates=["DATE1","DATE2"])
In [27]: df
Out[27]:
DATE1 DATE2
0 2020-07-08 23:54:17 2020-07-09 19:00:56.997
1 2020-07-08 08:22:28 2020-07-08 13:23:10.363
2 2020-07-08 10:24:25 2020-07-08 13:25:30.899
3 2020-07-08 20:19:35 2020-07-09 18:57:07.690
4 2020-07-08 06:07:45 2020-07-08 13:20:49.996
5 2020-07-08 10:20:25 2020-07-08 13:25:20.039
6 2020-07-08 19:18:23 2020-07-09 18:56:06.655
7 2020-07-08 22:12:03 2020-07-09 18:59:11.625
8 2020-07-08 09:38:44 2020-07-08 13:24:44.982
9 2020-07-08 09:54:44 2020-07-08 13:24:45.375
10 2020-07-08 06:23:45 2020-07-08 13:21:05.515
11 2020-07-08 18:49:17 2020-07-09 18:55:41.971
12 2020-07-08 19:47:23 2020-07-09 18:56:37.769
13 2020-07-08 10:48:25 2020-07-08 13:25:45.006
14 2020-07-08 05:30:45 2020-07-08 13:20:15.892
15 2020-07-08 06:09:45 2020-07-08 13:20:54.981
In [28]: df["diff_hours"] = (df.DATE2-df.DATE1).astype('timedelta64[h]')
In [29]: df
Out[29]:
DATE1 DATE2 diff_hours
0 2020-07-08 23:54:17 2020-07-09 19:00:56.997 19.0
1 2020-07-08 08:22:28 2020-07-08 13:23:10.363 5.0
2 2020-07-08 10:24:25 2020-07-08 13:25:30.899 3.0
3 2020-07-08 20:19:35 2020-07-09 18:57:07.690 22.0
4 2020-07-08 06:07:45 2020-07-08 13:20:49.996 7.0
5 2020-07-08 10:20:25 2020-07-08 13:25:20.039 3.0
6 2020-07-08 19:18:23 2020-07-09 18:56:06.655 23.0
7 2020-07-08 22:12:03 2020-07-09 18:59:11.625 20.0
8 2020-07-08 09:38:44 2020-07-08 13:24:44.982 3.0
9 2020-07-08 09:54:44 2020-07-08 13:24:45.375 3.0
10 2020-07-08 06:23:45 2020-07-08 13:21:05.515 6.0
11 2020-07-08 18:49:17 2020-07-09 18:55:41.971 24.0
12 2020-07-08 19:47:23 2020-07-09 18:56:37.769 23.0
13 2020-07-08 10:48:25 2020-07-08 13:25:45.006 2.0
14 2020-07-08 05:30:45 2020-07-08 13:20:15.892 7.0
15 2020-07-08 06:09:45 2020-07-08 13:20:54.981 7.0
In [30]: df["status"] = df["diff_hours"] > 24
In [31]: df
Out[31]:
DATE1 DATE2 diff_hours status
0 2020-07-08 23:54:17 2020-07-09 19:00:56.997 19.0 False
1 2020-07-08 08:22:28 2020-07-08 13:23:10.363 5.0 False
2 2020-07-08 10:24:25 2020-07-08 13:25:30.899 3.0 False
3 2020-07-08 20:19:35 2020-07-09 18:57:07.690 22.0 False
4 2020-07-08 06:07:45 2020-07-08 13:20:49.996 7.0 False
5 2020-07-08 10:20:25 2020-07-08 13:25:20.039 3.0 False
6 2020-07-08 19:18:23 2020-07-09 18:56:06.655 23.0 False
7 2020-07-08 22:12:03 2020-07-09 18:59:11.625 20.0 False
8 2020-07-08 09:38:44 2020-07-08 13:24:44.982 3.0 False
9 2020-07-08 09:54:44 2020-07-08 13:24:45.375 3.0 False
10 2020-07-08 06:23:45 2020-07-08 13:21:05.515 6.0 False
11 2020-07-08 18:49:17 2020-07-09 18:55:41.971 24.0 False
12 2020-07-08 19:47:23 2020-07-09 18:56:37.769 23.0 False
13 2020-07-08 10:48:25 2020-07-08 13:25:45.006 2.0 False
14 2020-07-08 05:30:45 2020-07-08 13:20:15.892 7.0 False
15 2020-07-08 06:09:45 2020-07-08 13:20:54.981 7.0 False
How to calculate time difference between two pandas column
You need omit pd.Timedelta
, because difference of times return timedeltas:
df_time['td'] = df_time['stop']-df_time['start']
print (df_time)
start stop td
0 2015-11-04 10:12:00 2015-11-06 06:38:00 1 days 20:26:00
1 2015-11-04 10:23:00 2015-11-05 08:30:00 0 days 22:07:00
2 2015-11-04 14:01:00 2015-11-17 10:34:00 12 days 20:33:00
EDIT: Another solution is subtract numpy arrays:
df_time['td'] = df_time['stop'].values - df_time['start'].values
print (df_time)
start stop td
0 2015-11-04 10:12:00 2015-11-06 06:38:00 1 days 20:26:00
1 2015-11-04 10:23:00 2015-11-05 08:30:00 0 days 22:07:00
2 2015-11-04 14:01:00 2015-11-17 10:34:00 12 days 20:33:00
Pandas: Calculate time in minutes between 2 columns, excluding weekends, public holidays and taking business hours into account
Use:
from functools import partial
# Convert strings to datetime
df['Start'] = pd.to_datetime(df['Start'])
df['End'] = pd.to_datetime(df['End'])
# Get holidays list
years = range(df['Start'].min().year, df['End'].max().year+1)
holidaylist = pyholidays.ZA(years=years).keys()
# Create a partial function as a shortcut
bduration = partial(bd.businessDuration,
starttime=Bus_start_time, endtime=Bus_end_time,
holidaylist=holidaylist, unit=unit)
# Compute business duration
df['TimeAdj'] = df.apply(lambda x: bduration(x['Start'], x['End']), axis=1)
Output:
>>> df
ID Start End TimeAdj
0 10 2022-01-01 07:00:00 2022-01-08 15:00:00 2700.0
1 11 2022-01-02 18:00:00 2022-01-10 15:30:00 3150.0
2 12 2022-01-01 09:15:00 2022-01-08 12:00:00 2700.0
3 13 2022-01-07 13:00:00 2022-01-23 17:00:00 5640.0
Time difference between two columns in Pandas
Convert both time columns to datetimes, get difference, convert to seconds by Series.dt.total_seconds
and then to minutes by division by 60
:
df['diff'] = (pd.to_datetime(df.time_ordered, format='%I:%M:%S %p')
.sub(pd.to_datetime(df.time_delivered, format='%I:%M:%S %p'))
.dt.total_seconds()
.div(60))
How to calculate the time difference in a data frame in python?
Assuming this is your dataset
data = {'date': ['2020/06/24', '2020/06/25', '2020/06/27', '2020/06/30'],
'time': ['23:00:28', '09:10:55', '03:42:58','16:45:51']}
df = pd.DataFrame(data)
print(df)
date time
0 2020/06/24 23:00:28
1 2020/06/25 09:10:55
2 2020/06/27 03:42:58
3 2020/06/30 16:45:51
You can use pandas .diff
after converting your data to proper datetime format using pd.to_datetime
df['date_time'] = pd.to_datetime(df['date'] + ' ' + df['time'])
df['time_diff'] = df['date_time'].diff()
print(df)
date time date_time time_diff
0 2020/06/24 23:00:28 2020-06-24 23:00:28 NaT
1 2020/06/25 09:10:55 2020-06-25 09:10:55 0 days 10:10:27
2 2020/06/27 03:42:58 2020-06-27 03:42:58 1 days 18:32:03
3 2020/06/30 16:45:51 2020-06-30 16:45:51 3 days 13:02:53
Related Topics
How to Declare Custom Exceptions in Modern Python
Python Lambda'S Binding to Local Values
How to Split a Huge CSV File Based on Content of First Column
Opening a Python Thread in a New Console Window
Pip Error:'Module' Object Has No Attribute 'Cryptography_Has_Ssl_St'
Why Is Signal.Sigalrm Not Working in Python on Windows
Multiprocessing Module Showing Memory for Each Child Process Same as Main Process
Python Parsing Log File to Extract Events in Real Time
Need a Way to Determine If a File Is Done Being Written To
Process Dies, If It Is Run via Paramiko Ssh Session and with "&" in the End
Difference Between Python'S Generators and Iterators
Convert Timestamps With Offset to Datetime Obj Using Strptime
When Using Os.Execlp, Why 'Python' Needs 'Python' as Argv[0]
Oserror: [Error 1] Operation Not Permitted
Installing Python 2.7 Without Root
High Kernel CPU When Running Multiple Python Programs