splitting time (h,m,s) from dataframe column - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: splitting time (h,m,s) from dataframe column (/thread-13300.html) |
splitting time (h,m,s) from dataframe column - dedaelfl - Oct-09-2018 Hi, I'm kind of new to python and I have been trying to split a time column in order to convert it all into a single unit but my splitting function does not work. mydata= pd.read_excel("myclock.xlsx",sheet_name="mytime",skiprows=0, index_col = [1]) Name ID Date Check In Check Out Worker_one 00001 Friday, September 28, 2018 9:50:35 18:50:23 Worker_two 00002 Friday, September 28, 2018 10:43:03 19:54:34 Worker_tree 00003 Friday, September 28, 2018 9:37:45 18:38:01 Worker_four 00004 Friday, September 28, 2018 8:44:07 17:30:04 Worker_five 00005 Friday, September 28, 2018 8:30:54 17:30:30 Worker_six 00006 Friday, September 28, 2018 9:01:43 18:04:50 Worker_seven 00007 Friday, September 28, 2018 8:34:16 17:34:50 Worker_eight 00008 Friday, September 28, 2018 11:57:43 20:58:54 Worker_nine 00009 Friday, September 28, 2018 9:03:53 18:40:43 Worker_ten 00010 Friday, September 28, 2018 8:23:04 18:45:32 from datetime import time : try: hours, minutes, seconds = time_str.split(":") except ValueError: return return int(hours)*60+ int(minutes) + int(seconds)/60.0 Check_Out_Minutes = mydata['Check Out'].apply(time_to_minutes) And it returns an attribute error --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-13-d84e37515c72> in <module>() ----> 1 Check_Out_Minutes = mydata['Check Out'].apply(time_to_minutes) C:\Program Files\Anaconda3\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwds) 2549 else: 2550 values = self.asobject -> 2551 mapped = lib.map_infer(values, f, convert=convert_dtype) 2552 2553 if len(mapped) and isinstance(mapped[0], Series): pandas/_libs/src/inference.pyx in pandas._libs.lib.map_infer() <ipython-input-5-c033186c9b10> in time_to_minutes(time_str) 1 def time_to_minutes(time_str): 2 try: ----> 3 hours, minutes, seconds = time_str.split(':') 4 except ValueError: 5 return AttributeError: 'datetime.time' object has no attribute 'split' RE: splitting time (h,m,s) from dataframe column - perfringo - Oct-09-2018 One way to get datetime.time object from string and access hours, minutes and seconds: >>> import datetime >>> check_in = '9:50:35' >>> check_in_time = datetime.datetime.strptime(check_in, '%H:%M:%S').time() >>> check_in_time.hour 9 >>> check_in_time.minute 50 >>> check_in_time.second 35 >>> check_in_time datetime.time(9, 50, 35) RE: splitting time (h,m,s) from dataframe column - volcano63 - Oct-09-2018 (Oct-09-2018, 07:05 AM)dedaelfl Wrote:Check_Out_Minutes = mydata['Check Out'].apply(time_to_minutes) As the exception message states, mydata['Check Out'] Series contain objects of type datetime.time - and you try to treat them as strings.Change your function to calculate time from datetime.time object - and you are set
|