0

I have a dataframe which contains a datetime column like this:

raw data

As you see in the "date_time" column the smallest time unit is minute. In fact, it does not have second uinte. I mean, for example, in the first six rows, 4:24 is repeated which means data gathered every 10 seconds or 4:25 repeated 10 times which means data recorded every 6 seconds.

Indeed, I am looking for a solution to have second in the "date_time" column.

The desirable format is like this:

desirable format

Asa Ya
  • 61
  • 1
  • 7

1 Answers1

1

Just use to_datetime() method of pandas

Solution:-

df['date_time']=pd.to_datetime(df['date_time'])

Then use apply() method:-

df['date_time']=df['date_time'].apply(lambda x:x.strftime("%H:%M:%S"))
Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41
  • could you please read the question again? I did what you suggested, it does just set the column as a datetime. While, I need to have second. I am going to calculate speed from second but the smallest unit is minute which leads to speed =0. – Asa Ya Feb 27 '21 at 06:38
  • Hey!!! Now Check My Answer...I Updated It @asadyarahmadi – Anurag Dabas Feb 27 '21 at 07:06
  • thanks for your reply, I performed your update but " 'str' object has no attribute 'strftime' " was returened!!. – Asa Ya Feb 27 '21 at 08:47
  • you are applying my update on your dataframe directly without converting your `'date_time'` to datetime object....that's why you are getting this error.......apply my solution from beginning not from 2nd step @asadyarahmadi – Anurag Dabas Feb 27 '21 at 08:54
  • Thanks really for spending time to help me out. Actually, I manipulate your code before running. I figured out if two times perform the first part of your code it returns that error. Anyway, according to your comments, I changed the code but unfortunately nothing changed :( . Thanks @Anurag Dabas – Asa Ya Feb 27 '21 at 09:20
  • run again I edited it...It is giving error because I forgot to add a `'` in `df['date_time']` so now I changed It...Now It will definately Work @asadyarahmadi – Anurag Dabas Feb 27 '21 at 09:23
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/229281/discussion-between-asadyarahmadi-and-anurag-dabas). – Asa Ya Feb 27 '21 at 09:43
  • This it the final solution so noting left to discuss @asadyarahmadi – Anurag Dabas Feb 27 '21 at 09:47