How to apply a ceiling to pandas DateTime

Suppose I have a pandas framework with a column whose values ​​are datetime64[ns].

Out[204]: 
0   2015-03-20 00:00:28
1   2015-03-20 00:01:44
2   2015-03-20 00:02:55
3   2015-03-20 00:03:39
4   2015-03-20 00:04:32
5   2015-03-20 00:05:52
6   2015-03-20 00:06:36
7   2015-03-20 00:07:44
8   2015-03-20 00:08:56
9   2015-03-20 00:09:47
Name: DateTime, dtype: datetime64[ns]

Is there an easy way to convert them to the nearest minute after time? that is, I want the following:

Out[204]: 
0   2015-03-20 00:01:00
1   2015-03-20 00:02:00
2   2015-03-20 00:03:00
3   2015-03-20 00:04:00
4   2015-03-20 00:05:00
5   2015-03-20 00:06:00
6   2015-03-20 00:07:00
7   2015-03-20 00:08:00
8   2015-03-20 00:09:00
9   2015-03-20 00:10:00
Name: DateTime, dtype: datetime64[ns]

I wrote complex code that first converts them to a string and then extracts three parts 00:09:47, converts them to integers, then if the last part (seconds) is already 00, I do the last part (seconds) to be 00add 1to the middle part ( minutes), except that the middle part (minutes) is narrower 59, in which case it adds to the first part (hours). Then recombining the new integers back in line and then restore back DateTime.

, . - ?

* *

@Jeff, @unutbu, . SO, .

+4
4

DataFrame dtype datetime64[ns],

df['date'] += np.array(-df['date'].dt.second % 60, dtype='<m8[s]')

.


,

import io
import sys
import numpy as np
import pandas as pd
StringIO = io.BytesIO if sys.version < '3' else io.StringIO

df = '''\
2015-03-20 00:00:00
2015-03-20 00:00:28
2015-03-20 00:01:44
2015-03-20 00:02:55
2015-03-20 00:03:39
2015-03-20 00:04:32
2015-03-20 00:05:52
2015-03-20 00:06:36
2015-03-20 00:07:44
2015-03-20 00:08:56
2015-03-20 00:09:47'''

df = pd.read_table(StringIO(df), sep='\s{2,}', 
                   header=None, parse_dates=[0], names=['date'])

df['date'] += np.array(-df['date'].dt.second % 60, dtype='<m8[s]')
print(df)

                  date
0  2015-03-20 00:00:00
1  2015-03-20 00:01:00
2  2015-03-20 00:02:00
3  2015-03-20 00:03:00
4  2015-03-20 00:04:00
5  2015-03-20 00:05:00
6  2015-03-20 00:06:00
7  2015-03-20 00:07:00
8  2015-03-20 00:08:00
9  2015-03-20 00:09:00
10 2015-03-20 00:10:00
+2

. ( ). .

In [46]: df.date+pd.to_timedelta(-df.date.dt.second % 60,unit='s')
Out[46]: 
0   2015-03-20 00:01:00
1   2015-03-20 00:02:00
2   2015-03-20 00:03:00
3   2015-03-20 00:04:00
4   2015-03-20 00:05:00
5   2015-03-20 00:06:00
6   2015-03-20 00:07:00
7   2015-03-20 00:08:00
8   2015-03-20 00:09:00
9   2015-03-20 00:10:00
dtype: datetime64[ns

. - . ( , , ). .

In [48]: pd.Series(pd.PeriodIndex(df.date.dt.to_period('T')+1).to_timestamp())
Out[48]: 
0   2015-03-20 00:01:00
1   2015-03-20 00:02:00
2   2015-03-20 00:03:00
3   2015-03-20 00:04:00
4   2015-03-20 00:05:00
5   2015-03-20 00:06:00
6   2015-03-20 00:07:00
7   2015-03-20 00:08:00
8   2015-03-20 00:09:00
9   2015-03-20 00:10:00
dtype: datetime64[ns]

"", .

+5

, , , , ( , .snap .rollforward, ):

ps = pd.Series([
        datetime(2015, 1, 1, 19, 18, 34), # roll up min, reset sec
        datetime(2015, 1, 1, 1, 1, 1), # roll up min, reset sec
        datetime(2015, 1, 1, 0, 0, 0), # do nothing
        datetime(2015, 1, 1, 23, 59, 1), # roll day/hr/min, reset sec
        datetime(2015, 1, 31, 23, 59, 1), # roll mth/day/hr/min, reset sec
        datetime(2015, 12, 31, 23, 59, 1) # roll yr/month/day/hr/min - reset sec
    ])
ps[ps.dt.second != 0] = ps.apply(lambda L: (L + timedelta(minutes=1)).replace(second=0))

:

0   2015-01-01 19:19:00
1   2015-01-01 01:02:00
2   2015-01-01 00:00:00
3   2015-01-02 00:00:00
4   2015-02-01 00:00:00
5   2016-01-01 00:00:00
+1

pandas ceil(). datetime Series.dt.ceil():

In[92]: t
Out[92]: 
0   2015-03-20 00:00:28
1   2015-03-20 00:01:44
2   2015-03-20 00:02:55
3   2015-03-20 00:03:39
4   2015-03-20 00:04:32
5   2015-03-20 00:05:52
6   2015-03-20 00:06:36
7   2015-03-20 00:07:44
8   2015-03-20 00:08:56
9   2015-03-20 00:09:47
dtype: datetime64[ns]

In[93]: t.dt.ceil('min')
Out[93]: 
0   2015-03-20 00:01:00
1   2015-03-20 00:02:00
2   2015-03-20 00:03:00
3   2015-03-20 00:04:00
4   2015-03-20 00:05:00
5   2015-03-20 00:06:00
6   2015-03-20 00:07:00
7   2015-03-20 00:08:00
8   2015-03-20 00:09:00
9   2015-03-20 00:10:00
dtype: datetime64[ns]

ceil() . .

+1

All Articles