如何将上限应用于 pandas DateTime [英] how to apply ceiling to pandas DateTime

查看:88
本文介绍了如何将上限应用于 pandas DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个pandas数据框,其列的值为datetime64[ns].

Suppose I have a pandas dataframe 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 any easy way to convert them the nearest minute after the time? i.e. 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]

我编写了一个复杂的代码,首先将它们转换为字符串,然后提取00:09:47的三个部分,将它们转换为整数,然后除非最后一部分(秒)已经为00,否则我将最后一部分(秒),将1添加到中间部分(分钟),除非中间部分(分钟)已经是59,在这种情况下,它会添加到第一部分(小时).然后将新的整数重新组合为字符串,然后重新构建DateTime.

I wrote a complicate code that first converts them to string and then extracts the three portions of 00:09:47, convert them into integers, then unless the last portion (seconds) is already 00, I make the last portion (seconds) to be 00, adds 1 to the middle portion (minutes) except if the middle portion (minutes) is already 59 in which case it adds to the first portion (hours). Then recombine the new integers back to a string and then reconstruct back the DateTime.

但是我在想,也许已经有一个现有的更简单的解决方案.有人有什么建议吗?

But I was thinking that may there might be already an existing simpler solution. Would anyone have any suggestions?

*编辑*

@ Jeff,@ unutbu,感谢您的回答.我只能在SO中选择一个答案,但两者都可以.

@Jeff, @unutbu, thanks for your answers. I can only select one answer in SO, but both work.

推荐答案

给出一个具有dtype datetime64[ns]列的DataFrame,您可以 使用

Given a DataFrame with a column of dtype datetime64[ns], you could use

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

添加适当的秒数以获得上限.

to add the appropriate number of seconds to obtain the ceiling.

例如,

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

这篇关于如何将上限应用于 pandas DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆