Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Set the time of datetime column with condition

I have a dataframe that it has a datetime column ‘started_at’

started_at
2019-12-03 10:33:06
2019-12-03 12:29:37
2019-12-03 18:29:37
2019-12-04 11:29:37

I would like to change the time to it, if the time is smaller that a specific time (12:00)

So, the data should become:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

started_at
2019-12-03 12:00:00
2019-12-03 12:29:37
2019-12-03 18:29:37
2019-12-04 12:00:00

Any suggestions?

>Solution :

Maybe you can try this

import datetime as dt
for i in range(len(df)):
    fmt = '%Y-%m-%d %H:%M:%S'
    val = dt.datetime.strptime(str(df.loc[i][0]), fmt )
    new_val = dt.datetime.strptime(str(df.loc[i][0]).split(" ")[0] + " 12:00:00", fmt )
    if val < new_val:
        df["started_at"] = df["started_at"].replace(val,new_val)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading