1

I have a DataFrame like this (date: datetime64[ns], v: float64)

date                   v
2024-09-01 22:09:55    1.2
2024-09-01 22:12:08    1.11
2024-09-01 22:59:59    1.7
2024-09-01 23:00:02    1.1
2024-09-01 23:04:00    2.2

what I want to have is if the minutes in datetime are <= 10 then substract 11 minutes (something like df['date']-timedelta(minutes=11)). Otherwise keep the original datetime.

The expected output should be

date                   v
2024-09-01 21:58:55    1.2
2024-09-01 22:12:08    1.11
2024-09-01 22:59:59    1.7
2024-09-01 22:49:02    1.1
2024-09-01 22:53:00    2.2

1 Answer 1

2

Use DataFrame.loc with test minutes by Series.dt.minute and subtract Timedelta:

df = pd.DataFrame({'date':pd.to_datetime(['2024-09-01 22:09:55',
                                          '2024-09-01 22:12:08',
                                          '2024-09-01 22:59:59',
                                          '2024-09-01 23:00:02',
                                          '2024-09-01 23:04:00']),
                   'v':[1.2,1.11,1.7,1.7,2.2]})

df.loc[df['date'].dt.minute <= 11, 'date'] -= pd.Timedelta('11 Min')
print (df)
                 date     v
0 2024-09-01 21:58:55  1.20
1 2024-09-01 22:12:08  1.11
2 2024-09-01 22:59:59  1.70
3 2024-09-01 22:49:02  1.70
4 2024-09-01 22:53:00  2.20
Sign up to request clarification or add additional context in comments.

1 Comment

beautiful! thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.