rounding up time to last 30 mins interval

Question:

I have a dataframe with a column ‘queue_ist_dt’. This column contains pandas._libs.tslibs.timestamps.Timestamp values. My requirement is :

if time = 10:13:00 then round_off_time = 10:00:00
if time = 23:29:00 then round_off_time = 23:00:00

and so on.

if time = 10:31:00 then round_off_time = 10:30:00
if time = 23:53:00 then round_off_time = 23:30:00

and so on.

if time = 10:30:00 then round_off_time = 10:30:00

These are the 3 conditions.

I tried to write the following logic :

for r in range(df.shape[0]):
            try:
                if df.loc[r,'queue_ist_dt'].minute<30:
                    timedelta = pd.Timedelta(minutes=df.loc[r,'queue_ist_dt'].minute)
                    df.loc[r,'queue_placed_interval'] = df.loc[r,'queue_ist_dt']- timedelta
                elif df.loc[r,'queue_ist_dt'].minute>30:
                    ******NEED HELP TO BUILD THIS LOGIC******
            except:
                pass

Need help to build logic for the time where minutes is greater than 30 mins and have to be rounded down to 30 mins.

Asked By: Soumya Pandey

||

Answers:

Use Series.dt.floor:

#if necessary convert to datetimes
df['queue_ist_dt'] = pd.to_datetime(df['queue_ist_dt'].astype(str)) 
    
df['queue_ist_dt1'] = df['queue_ist_dt'].dt.floor('30Min').dt.time
print (df)
Answered By: jezrael

Considering this dataframe df as example

df = pd.DataFrame({'queue_ist_dt': [pd.Timestamp('2021-01-01 10:00:00'),
                                    pd.Timestamp('2021-01-01 10:30:00'),
                                    pd.Timestamp('2021-01-01 11:00:00'),
                                    pd.Timestamp('2021-01-01 11:30:00'),
                                    pd.Timestamp('2021-01-01 23:00:00'),
                                    pd.Timestamp('2021-01-01 23:30:00'),
                                    pd.Timestamp('2021-01-01 23:30:00')]
                     })

[Out]:

         queue_ist_dt
0 2021-01-01 10:01:00
1 2021-01-01 10:35:00
2 2021-01-01 11:19:00
3 2021-01-01 11:33:00
4 2021-01-01 23:23:00
5 2021-01-01 23:22:00
6 2021-01-01 23:55:00

One way would be to use pandas.Series.dt.round as follows

df['round_off_time'] = df['queue_ist_dt'].dt.round('30min')

[Out]:

         queue_ist_dt      round_off_time
0 2021-01-01 10:01:00 2021-01-01 10:00:00
1 2021-01-01 10:35:00 2021-01-01 10:30:00
2 2021-01-01 11:19:00 2021-01-01 11:30:00
3 2021-01-01 11:33:00 2021-01-01 11:30:00
4 2021-01-01 23:23:00 2021-01-01 23:30:00
5 2021-01-01 23:22:00 2021-01-01 23:30:00
6 2021-01-01 23:55:00 2021-01-02 00:00:00

If the goal is to change the values in the column queue_ist_dt, do the following

df['queue_ist_dt'] = df['queue_ist_dt'].dt.round('30min')

[Out]:

         queue_ist_dt
0 2021-01-01 10:00:00
1 2021-01-01 10:30:00
2 2021-01-01 11:30:00
3 2021-01-01 11:30:00
4 2021-01-01 23:30:00
5 2021-01-01 23:30:00
6 2021-01-02 00:00:00
Answered By: Gonçalo Peres

Logic is subtract 30 minute from timedelta

code is as below:

for r in range(df.shape[0]):
            try:
                if df.loc[r,'queue_ist_dt'].minute<30:
                    timedelta = pd.Timedelta(minutes=df.loc[r,'queue_ist_dt'].minute)
                    df.loc[r,'queue_placed_interval'] = df.loc[r,'queue_ist_dt']- timedelta
                elif df.loc[r,'queue_ist_dt'].minute>30:
                    ******THIS LOGIC******
                    timedelta = pd.Timedelta(minutes=df.loc[r,'queue_ist_dt'].minute)
                    df.loc[r,'queue_placed_interval'] = df.loc[r,'queue_ist_dt']- (timedelta-30)
            except:
                pass

Let me know if this helps you

Answered By: Devam Sanghvi
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.