(Oct-09-2018, 07:05 AM)dedaelfl Wrote:Check_Out_Minutes = mydata['Check Out'].apply(time_to_minutes)
Output: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'
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 setOutput:In [22]: def time_2_mins(t):
...: return t.hour * 60 + t.minute + t.second / 60
...: times = pd.Series([time(12, 15, 40), time(8, 25, 6)])
...: print(times)
...: times.apply(time_2_mins)
...:
...:
0 12:15:40
1 08:25:06
dtype: object
Out[22]:
0 735.666667
1 505.100000
dtype: float64
Test everything in a Python shell (iPython, Azure Notebook, etc.)
- Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.