Soumya Pandey
Soumya Pandey

Reputation: 331

rounding up time to last 30 mins interval

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.

Upvotes: 2

Views: 179

Answers (3)

Devam Sanghvi
Devam Sanghvi

Reputation: 517

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

Upvotes: 1

jezrael
jezrael

Reputation: 862661

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)

Upvotes: 3

Gon&#231;alo Peres
Gon&#231;alo Peres

Reputation: 13582

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

Upvotes: 0

Related Questions