Reputation: 173
this how my dataset looks like ;-
Timestamp
I try to convert above timestamp using below functions
import datetime
def convert(date_time):
format = '%Y-%m-%d %H:%M:%S:%f' # The format
datetime_obj = datetime.datetime.strptime(date_time, format)
datetime_str = datetime_obj.strftime("%Y-%m-%d %H:%M:%S")
return datetime.datetime.strptime(datetime_str, "%Y-%m-%d %H:%M:%S")
df['Timestamp'] = df['Timestamp'].apply(convert)
df.head()
after running above code i get below error.
ValueError: time data '2020-11-1 24:0:47:40476' does not match format '%Y-%m-%d %H:%M:%S:%f'
how do i convert the date '2020-11-1 24:0:47:40476' --> '2020-11-2 0:0:47:40476'
Upvotes: 0
Views: 234
Reputation: 522165
You can parse the date and time separately, parsing the date part into just a date with the time component set to 0:00:00, then add the time part as timedelta
to it:
>>> from datetime import datetime, timedelta
>>> s = '2020-11-1 24:0:47:40476'
>>> d, t = s.split()
>>> d
'2020-11-1'
>>> t
'24:0:47:40476'
>>> ts = datetime.strptime(d, '%Y-%m-%d')
>>> ts
datetime.datetime(2020, 11, 1, 0, 0)
>>> h, m, s, ms = t.split(':')
>>> ts + timedelta(hours=int(h), minutes=int(m), seconds=int(s), milliseconds=int(ms))
datetime.datetime(2020, 11, 2, 0, 1, 27, 476000)
Note that this may or may not work as desired should DST transitions happen right during that time; it's a bit unclear how that's supposed to work.
Also note that the 40476 milliseconds (?) added up to additional minutes. It’s slightly unclear what exactly that number is supposed to represent, you may have to split that up into milli- and microseconds too before passing it to timedelta
.
Upvotes: 2