only keep time of datetime

User 5239 | 5/26/2016, 8:55:33 PM

I have an SArray with each element as an datetime object data['Date Reported'][0:5] dtype: datetime Rows: 5 [datetime.datetime(2015, 6, 28, 10, 31), datetime.datetime(2015, 6, 24, 11, 9), datetime.datetime(2015, 6, 22, 9, 22), datetime.datetime(2015, 6, 21, 15, 52), datetime.datetime(2015, 6, 20, 11, 59)]

I want to create an SArray with only the time of the datetime

  • data['Date Reported'][0].time() is working fine and returns datetime.time(10, 31)

  • but when I use the SArray.apply, there is an error data['Date Reported'][0:5].apply(lambda x: x.time()) RuntimeError: Runtime Exception. Exception in python callback function evaluation: AttributeError("'datetime.time' object has no attribute 'year'",)

  • however data['Date Reported'][0:5].apply(lambda x: x.date()) is working dtype: datetime Rows: 5 [datetime.datetime(2015, 6, 28, 0, 0), datetime.datetime(2015, 6, 24, 0, 0), datetime.datetime(2015, 6, 22, 0, 0), datetime.datetime(2015, 6, 21, 0, 0), datetime.datetime(2015, 6, 20, 0, 0)]

Any suggestion on this problem? Thanks.

Ding

Comments

User 5159 | 5/27/2016, 7:09:03 PM

Hi Ding,

This is because x.date() will return in type datetime.datetime, but x.time() will return in type datetime.time. there is no covert function in python from datetime.time to datetime.datetime. You can manually write a lamda convert function.