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

Changing A value on Time Column with Python/Pandas

I have a dataframe that contains +10K rows, first 4 rows is image below:

enter image description here

As you can see the hour can be different, and I am trying to replace only hour value to "00" by ‘for’ loop, but it takes a quiet time. Code is:

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

for i in range(0,len(task_df)):
    task_df["TARGET_END_DATE"].iloc[i]=pd.datetime(year=task_df["TARGET_END_DATE"].iloc[i].year,
                                                month=task_df["TARGET_END_DATE"].iloc[i].month,
                                                day=task_df["TARGET_END_DATE"].iloc[i].day,
                                                hour=0,
                                                minute=0)

Is there any quicker way to do it ?

>Solution :

If need only 00 hours solution is simplier/ faster with Series.dt.floor :

task_df["TARGET_END_DATE"] = task_df["TARGET_END_DATE"].dt.floor('d')

If need some hour, e.g. 10:00 add it:

task_df["TARGET_END_DATE"] = (task_df["TARGET_END_DATE"].dt.floor('d') + 
                                pd.Timedelta(10, 'hour'))
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