Python 如何将日期时间列舍入到最接近的一刻钟

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32344533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:25:09  来源:igfitidea点击:

How do I round datetime column to nearest quarter hour

pythondatetimepandaspython-datetime

提问by sfactor

I have loaded a data file into a Python pandas dataframe. I has a datetime column of the format 2015-07-18 13:53:33.280.

我已将数据文件加载到 Python pandas 数据帧中。我有一个格式的日期时间列2015-07-18 13:53:33.280

What I need to do is create a new column that rounds this out to its nearest quarter hour. So, the date above will be rounded to 2015-07-18 13:45:00.000.

我需要做的是创建一个新列,将其四舍五入到最接近的一刻钟。因此,上述日期将四舍五入为2015-07-18 13:45:00.000

How do I do this in pandas? I tried using the solution from here, but get an 'Series' object has no attribute 'year'error.

我如何在熊猫中做到这一点?我尝试使用此处的解决方案,但出现'Series' object has no attribute 'year'错误。

采纳答案by Anand S Kumar

Assuming that your series is made up of datetimeobjects, You need to use Series.apply. Example -

假设您的系列由datetime对象组成,您需要使用Series.apply. 例子 -

import datetime
df['<column>'] = df['<column>'].apply(lambda dt: datetime.datetime(dt.year, dt.month, dt.day, dt.hour,15*(dt.minute // 15)))

The above example to always round to the previous quarter hour (behavior similar to floor function).

上面的例子总是四舍五入到上一个刻钟(行为类似于 floor 函数)。

EDIT

编辑

To round to the correct quarter hour (as in , if its 7 mins 30 seconds past previous quarter, to show the next quarter) . We can use the below example -

四舍五入到正确的刻钟(如 ,如果它是上一季度的 7 分 30 秒,则显示下一个季度)。我们可以使用下面的例子 -

import datetime
df['<column>'] = df['<column>'].apply(lambda dt: datetime.datetime(dt.year, dt.month, dt.day, dt.hour,15*round((float(dt.minute) + float(dt.second)/60) / 15)))

The above would only take the latest seconds into consideration , if you want the millisecond/microsecond into consideration , you can add that to the above equation as - (float(dt.minute) + float(dt.second)/60 + float(dt.microsecond)/60000000)

上面只会考虑最近的秒数,如果你想考虑毫秒/微秒,你可以将它添加到上面的等式中 - (float(dt.minute) + float(dt.second)/60 + float(dt.microsecond)/60000000)

回答by Eric Blum

Anand S Kumar's answer doesn't round to the nearest quarter hour, it cuts off the minutes to the nearest 15 minutes below it.

Anand S Kumar 的回答没有四舍五入到最近的四分之一小时,而是将分钟截断到它下面最近的 15 分钟。

Actually, in your example 2015-07-18 13:53:33.280should round to 2015-07-18 14:00:00.000since 53:33.280is closer to 60 minutes than 45 minutes.

实际上,在您的示例中2015-07-18 13:53:33.280应该舍入到2015-07-18 14:00:00.000因为53:33.280比 45 分钟更接近 60 分钟。

I found an more robust answer for rounding in this post.

我在这篇文章中找到了一个更可靠的四舍五入答案。

For your situation this should work:

对于您的情况,这应该有效:

import datetime

def round_time(time, round_to):
    """roundTo is the number of minutes to round to"""
    rounded = time + datetime.timedelta(minutes=round_to/2.)
    rounded -= datetime.timedelta(minutes=rounded.minute % round_to,
                                  seconds=rounded.second,
                                  microseconds=rounded.microsecond)
    return rounded

dt['dtcolumn'] = df['dtcolumn'].apply(lambda x: round_time(x))

回答by tworec

You can use round(freq). There is also a shortcut column.dtfor datetime functions access (as @laurens-koppenol suggests).

您可以使用round(freq). 还有一个column.dt用于访问日期时间函数的快捷方式(如@laurens-koppenol 所建议的那样)。

Here's one-liner:

这是单线:

df['old column'].dt.round('15min')  

String aliases for valid frequencies can be found here. Full working example:

可以在此处找到有效频率的字符串别名。完整的工作示例:

In [1]: import pandas as pd    
In [2]: df = pd.DataFrame([pd.Timestamp('2015-07-18 13:53:33.280'),
                           pd.Timestamp('2015-07-18 13:33:33.330')],
                         columns=['old column'])

In [3]: df['new column']=df['old column'].dt.round('15min')  
In [4]: df
Out[4]: 
               old column          new column
0 2015-07-18 13:53:33.280 2015-07-18 14:00:00
1 2015-07-18 13:33:33.330 2015-07-18 13:30:00

回答by Laurens Koppenol

This looks a little nicer

这个好看一点

column.dt.allows the datetime functions for datetime columns, like column.str.does for string-like columns

column.dt.允许日期时间列的日期时间函数,就像column.str.字符串一样的列

datetime-like properties API reference

类似日期时间的属性 API 参考

import pandas as pd

# test df
df = pd.DataFrame([{'old_column':pd.Timestamp('2015-07-18 13:53:33.280')}])

df['new_column'] = df['old_column'].dt.round('15min')

df