datetime - date difference in hours with python (excel data import) -
i need calculate hour difference between 2 dates (format: year-month-daythh:mm:ss potentially transform data format (format: year-month-day hh:mm:ss) huge excel file. efficient way in python? have tried use datatime/time object (typeerror: expected string or buffer), timestamp (valueerror) , dataframe (does not give hour result).
thank help!
excel file:
order_date received_customer column3 2000-10-06t13:00:58 2000-11-06t13:00:58 1 2000-10-21t15:40:15 2000-12-27t10:09:29 2 2000-10-23t10:09:29 2000-10-26t10:09:29 3 ..... ....
datatime/time object code (typeerror: expected string or buffer):
import pandas pd import time t data=pd.read_excel('/path/file.xlsx') s1 = (data,['order_date']) s2 = (data,['received_customer']) s1time = t.strptime(s1, "%y:%m:%d:%h:%m:%s") s2time = t.strptime(s2, "%y:%m:%d:%h:%m:%s") deltainhours = (t.mktime(s2time) - t.mktime(s1time)) print deltainhours, "hours"
timestamp (valueerror) code:
import pandas pd import datetime dt data=pd.read_excel('/path/file.xlsx') df = pd.dataframe(data,columns=['order_date','received_customer']) df.to = [pd.timestamp('order_date')] df.fr = [pd.timestamp('received_customer')] (df.fr-df.to).astype('timedelta64[h]')
dataframe (does not return desired result)
import pandas pd data=pd.read_excel('/path/file.xlsx') df = pd.dataframe(data,columns=['order_date','received_customer']) df['order_date'] = pd.to_datetime(df['order_date']) df['received_customer'] = pd.to_datetime(df['received_customer']) answer = df.dropna()['order_date'] - df.dropna()['received_customer'] answer.astype('timedelta64[h]') print(answer) out: 0 24 days 16:38:07 1 0 days 00:00:00 2 20 days 12:39:52 dtype: timedelta64[ns] should smth. that: 0 592 hour 1 0 hour 2 492 hour
is there way convert timedelta64[ns]
hours answer.astype('timedelta64[h]')
?
for each of solutions mixed datatypes , methods. whereas not find time explicitly explain mistakes, yet want providing (probably non optimal) solution. built solution out of previous tries , combined knowledge other questions such as:
convert timedelta days, hours , minutes
get total number of hours pandas timedelta?
note used python 3. hope solution guides way. solution one:
import pandas pd datetime import datetime import numpy np d = pd.read_excel('c:\\users\\nrieble\\desktop\\check.xlsx',header=0) start = [pd.to_datetime(e) e in data['order_date'] if len(str(e))>4] end = [pd.to_datetime(e) e in data['received_customer'] if len(str(e))>4] delta = np.asarray(s2time)-np.asarray(s1time) deltainhours = [e/np.timedelta64(1, 'h') e in delta] print (deltainhours, "hours")
Comments
Post a Comment