Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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😊

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading