Home > front end >  Changing A value on Time Column with Python/Pandas
Changing A value on Time Column with Python/Pandas

Time:01-13

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:

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 ?

CodePudding user response:

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'))

CodePudding user response:

You can use:

task_df["TARGET_END_DATE"].dt.normalize()
  •  Tags:  
  • Related