Home > OS >  Add one day for certain time
Add one day for certain time

Time:09-22

I want to update my date column with certain times because some of the dates are not correct. For some reason, they key in the date with time between 00:00 and 7:30 with the day before.

For example:

          date   time
0   01-01-2022  01:00
1   01-01-2022  20:00
2   01-05-2022  03:00
2   01-07-2022  06:00

Which supposes to be like this:

          date   time
0   01-02-2022  01:00
1   01-01-2022  20:00
2   01-06-2022  03:00
2   01-08-2022  06:00

I know I can update all of dates with this code.

df["date"] = pd.to_datetime(df["date"])   datetime.timedelta(days=1)

But I have no idea how to only update certain rows I want.

Does anyone know how to update the date column?

CodePudding user response:

Sure, if you want to do it in place (modifying the original df):

import datetime

df.loc[df['date'].dt.time <= datetime.time(7, 30), 'date']  = pd.Timedelta('1D')

Edit: choose < or <= appropriately depending on your need (right-open or right-close interval, respectively).

CodePudding user response:

try:

data.loc[[0], ['date']] = data.loc[[0], ['date']]   datetime.timedelta(days=1)  

CodePudding user response:

while the other answers are correct and efficient, i'd like to add the long answer using a for loop and an if statement

for i in range(len(df)):
    if pd.to_datetime(df["time"][i]) > pd.to_datetime('00:00') and pd.to_datetime(df["time"][i]) < pd.to_datetime("7:30"):
        df["date"][i] = pd.to_datetime(df["date"][i])   datetime.timedelta(days=1)

        # this line is to cut he extra time added after the addition operation
        df['date'][i] = df['date'][i].strftime('%d-%m-%Y') # try to remove it and see for yourself what it does

input:

   date        time
0  01-02-2022  01:00
1  01-02-2022  01:00
2  01-02-2022  11:00

output:

         date   time
0  01-03-2022  01:00
1  01-03-2022  01:00
2  01-02-2022  11:00
  • Related