pandas 熊猫数据框如何在忽略日期的同时比较日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45586012/
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
pandas dataframe how to compare datetime while ignoring the date
提问by Hyman
The built-in functionality of datetime supports comparing two datetime.datetime objects directly using '< > =...'. However, I would like to compare the two datetime with respect to only the hour and minute.
datetime 的内置功能支持使用“< > =...”直接比较两个 datetime.datetime 对象。但是,我想仅就小时和分钟来比较两个日期时间。
For example, if we have '2016-07-01 11:00:00' and '2017-07-01 09:00:00', I want to say that '2016-07-01 11:00:00' is greater since 11:00 > 9:00. I did not see any built-in functionality that we can use.
例如,如果我们有'2016-07-01 11:00:00'和'2017-07-01 09:00:00',我想说'2016-07-01 11:00:00'是大于 11:00 > 9:00。我没有看到我们可以使用的任何内置功能。
Instead, I did things like comparing each row whether
相反,我做了一些事情,比如比较每一行是否
data = data[time_start.hour * 60 + time_start.minute
< (data['time'].hour * 60 + data['time'].minute)
< time_end.hour * 60 + time_end.minute ]
But there is error:
但是有错误:
AttributeError: 'Series' object has no attribute 'hour'
I am using python 2.7, is this also a problem in python 3?
我正在使用 python 2.7,这也是 python 3 中的问题吗?
What would be a good way of doing such comparison? Thanks!
进行这种比较的好方法是什么?谢谢!
回答by DeepSpace
I am using python 2.7, is this also a problem in python 3?
我正在使用 python 2.7,这也是 python 3 中的问题吗?
This has nothing to do with the Python version.
这与 Python 版本无关。
If you are using pandas > 0.16 you will need to use the dt
accessor:
如果您使用的是 pandas > 0.16,则需要使用dt
访问器:
data['time'].dt.hour
and data['time'].dt.minute
data['time'].dt.hour
和 data['time'].dt.minute
For example:
例如:
import pandas as pd
import datetime as dt
df = pd.DataFrame({'a': [dt.datetime(2016, 7, 1, 11), dt.datetime(2017, 7, 7, 9)]})
df['b'] = df['a'].dt.hour
print(df)
# a b
# 0 2016-07-01 11:00:00 11
# 1 2017-07-07 09:00:00 9
df = df[df['a'].dt.hour > 10]
print(df)
# a b
# 0 2016-07-01 11:00:00 11
回答by Michael H.
If the input dates is a str and are arranged from yyyy-mm-dd hh:mm:ss, why don't just compare this as a string.
如果输入日期是一个 str 并且是从 yyyy-mm-dd hh:mm:ss 排列的,为什么不把它作为一个字符串进行比较。
import pandas as pd
dates = [ '2016-07-01 11:00:00','2016-07-01 13:00:00','2016-07-01 15:00:00']
df = pd.DataFrame(dates,columns=['dates'])
a = (df['dates'].str[-8:] > '09:00:00') & (df['dates'].str[-8:] <= '11:00:00')
print(df [a])