Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round up a Pandas Timestamp

Tags:

python

pandas

I have a list of Pandas Timestamps with me. It is supposed to be 15 mints seperated, but due to some error in measurements there may be mistakes in minutes.

I want to round up the Timestamp to next nearest 15 minutes.

I know I can use Timestamp.round() from pandas to round the Timestamp. But the problem with it is it rounds to nearest 15 mints. I always want to round up to next 15 minutes.

For example,

If I have a timestamp as given below,

ts = pd.Timestamp('2017-12-31 23:50:00+0530')

rounded = ts.round(freq='15T')

The result I will get is Timestamp('2017-12-31 23:45:00+0530', tz='pytz.FixedOffset(330)').

I dont want this to happen, I want the result to be next 15 minute timestamp ie it must be

Timestamp('2018-01-01 00:00:00+0530', tz='pytz.FixedOffset(330)')

At the same time, I will have timestamps of the form

Timestamp('2018-01-01 00:00:00+0530', tz='pytz.FixedOffset(330)')

If I am using Timestamp.ceil for my purpose, I will get an output like this

Timestamp('2018-01-01 00:15:00+0530', tz='pytz.FixedOffset(330)')

I don't want it to be happening.

The output must be

 Timestamp('2018-01-01 00:00:00+0530', tz='pytz.FixedOffset(330)')

In short, If the time stamp is correct no cieling must be done and if the timestam is not at 15 mints it must be ceiled.

How can I do this with pandas easily.?

like image 793
Sreeram TP Avatar asked Dec 11 '22 05:12

Sreeram TP


1 Answers

You want ceil, not round:

ts.ceil(freq='15T')

Timestamp('2018-01-01 00:00:00+0530', tz='pytz.FixedOffset(330)')
like image 142
user3483203 Avatar answered Dec 12 '22 19:12

user3483203