resta de la columna de fecha de pandas

Tengo un dataframe de pandas como este ..

created_time reached_time 2016-01-02 12:57:44 14:20:22 2016-01-02 12:57:44 13:01:38 2016-01-03 10:38:51 12:24:07 2016-01-03 10:38:51 12:32:11 2016-01-03 10:38:52 12:23:20 2016-01-03 10:38:52 12:51:34 2016-01-03 10:38:52 12:53:33 2016-01-03 10:38:52 13:04:08 2016-01-03 10:38:52 13:13:40 

Quiero restar estas dos columnas de fecha y quiero obtener time

Lo estoy haciendo siguiendo en python.

 speed['created_time'].dt.time - speed['reached_time'] 

Pero me da el siguiente error.

TypeError: ufunc subtract cannot use operands with types dtype('O') and dtype('<m8[ns]')

el tipo de datos de created_time es object y el tipo de datos de reached_type es timedelta64[ns]

Podrías desplegar en las matrices NumPy y hacer la aritmética datetime / timedelta allí. Primero, crea una matriz de fechas de dtype datetime64[D] :

 dates = speed['created_time'].values.astype('datetime64[D]') 

Luego tienes dos opciones: puedes convertir el tiempo reached_time en fechas y restar las fechas de las fechas:

 speed['reached_date'] = dates + speed['reached_time'].values speed['diff'] = speed['created_time'] - speed['reached_date'] 

o puedes convertir created_time a timedeltas, y restar timedeltas de timedeltas:

 speed['created_delta'] = speed['created_time'].values - dates speed['diff'] = speed['created_delta'] - speed['reached_time'] 

 import pandas as pd speed = pd.DataFrame( {'created_time': ['2016-01-02 12:57:44', '2016-01-02 12:57:44', '2016-01-03 10:38:51', '2016-01-03 10:38:51', '2016-01-03 10:38:52', '2016-01-03 10:38:52', '2016-01-03 10:38:52', '2016-01-03 10:38:52', '2016-01-03 10:38:52'], 'reached_time': ['14:20:22', '13:01:38', '12:24:07', '12:32:11', '12:23:20', '12:51:34', '12:53:33', '13:04:08', '13:13:40']}) speed['reached_time'] = pd.to_timedelta(speed['reached_time']) speed['created_time'] = pd.to_datetime(speed['created_time']) dates = speed['created_time'].values.astype('datetime64[D]') speed['reached_date'] = dates + speed['reached_time'].values speed['diff'] = speed['created_time'] - speed['reached_date'] # alternatively # speed['created_delta'] = speed['created_time'].values - dates # speed['diff'] = speed['created_delta'] - speed['reached_time'] print(speed) 

rendimientos

  created_time reached_time reached_date diff 0 2016-01-02 12:57:44 14:20:22 2016-01-02 14:20:22 -1 days +22:37:22 1 2016-01-02 12:57:44 13:01:38 2016-01-02 13:01:38 -1 days +23:56:06 2 2016-01-03 10:38:51 12:24:07 2016-01-03 12:24:07 -1 days +22:14:44 3 2016-01-03 10:38:51 12:32:11 2016-01-03 12:32:11 -1 days +22:06:40 4 2016-01-03 10:38:52 12:23:20 2016-01-03 12:23:20 -1 days +22:15:32 5 2016-01-03 10:38:52 12:51:34 2016-01-03 12:51:34 -1 days +21:47:18 6 2016-01-03 10:38:52 12:53:33 2016-01-03 12:53:33 -1 days +21:45:19 7 2016-01-03 10:38:52 13:04:08 2016-01-03 13:04:08 -1 days +21:34:44 8 2016-01-03 10:38:52 13:13:40 2016-01-03 13:13:40 -1 days +21:25:12 

Usando la mejora de HRYR , puede realizar el cálculo sin tener que descender a las matrices NumPy (es decir, no es necesario acceder a los .values ):

 dates = speed['created_time'].dt.normalize() speed['reached_date'] = dates + speed['reached_time'] speed['diff'] = speed['created_time'] - speed['reached_date'] 

Convertir la columna created_time a datetime primero:

 df["created_time"] = pd.to_datetime(df["created_time"]) 

Luego use df["created_time"] - df["created_time"].dt.normalize() para obtener la parte de tiempo como timedelta type.