pandas 如何使用数据帧 between_time() 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24576786/
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-09-13 22:13:37  来源:igfitidea点击:

how to use dataframe between_time() function

pythondatetimepandas

提问by lserlohn

I am trying to use the between_timefunction. I have formatted the string type time to datetime

我正在尝试使用该between_time功能。我已将字符串类型时间格式化为日期时间

dataset['TimeStamp'] = pd.to_datetime(dataset['TimeStamp'],format)

and I defined search start time and end time:

我定义了搜索开始时间和结束时间:

start = datetime.time(9,40,0)

end = datetime.time(10,00,0)

then I call dataset['TimeStamp'].between_time(start, end)

然后我打电话 dataset['TimeStamp'].between_time(start, end)

This is the error I get:

这是我得到的错误:

TypeError: Index must be DatetimeIndex

Please how can I fix it. Thank you

请问我该如何解决。谢谢

回答by furas

Example - I use info from comments:

示例 - 我使用评论中的信息:

import pandas as pd
import StringIO
import datetime

data = '''time --- value
1984-12-12 14:08:00 --- 1
1984-12-12 14:25:00 --- 2
1984-12-12 14:47:00 --- 4
1984-12-12 16:37:00 --- 3
1984-12-12 16:37:00 --- 9
1984-12-12 16:37:00 --- 5
1984-12-12 17:52:00 --- 3
1984-12-12 17:52:00 --- 7
1984-12-12 19:29:00 --- 2'''

#------------------------------------------------

df = pd.read_csv(StringIO.StringIO(data), sep=' --- ')

df['time'] = pd.DatetimeIndex(df['time'])

print "\nDataFrame:\n", df 

print '\nIndex:', type(df.index)

#------------------------------------------------

df.set_index(keys='time', inplace=True)

print "\nDataFrame:\n", df 

print '\nIndex:', type(df.index)

#------------------------------------------------

start = datetime.time(14,50,0)
end = datetime.time(18,0,0)

print "\nResult:\n", df['value'].between_time(start, end)

Results:

结果:

DataFrame:
                 time  value
0 1984-12-12 14:08:00      1
1 1984-12-12 14:25:00      2
2 1984-12-12 14:47:00      4
3 1984-12-12 16:37:00      3
4 1984-12-12 16:37:00      9
5 1984-12-12 16:37:00      5
6 1984-12-12 17:52:00      3
7 1984-12-12 17:52:00      7
8 1984-12-12 19:29:00      2

Index: <class 'pandas.core.index.Int64Index'>

DataFrame:
                     value
time                      
1984-12-12 14:08:00      1
1984-12-12 14:25:00      2
1984-12-12 14:47:00      4
1984-12-12 16:37:00      3
1984-12-12 16:37:00      9
1984-12-12 16:37:00      5
1984-12-12 17:52:00      3
1984-12-12 17:52:00      7
1984-12-12 19:29:00      2

Index: <class 'pandas.tseries.index.DatetimeIndex'>

Result:
time
1984-12-12 16:37:00    3
1984-12-12 16:37:00    9
1984-12-12 16:37:00    5
1984-12-12 17:52:00    3
1984-12-12 17:52:00    7
Name: value, dtype: int64