比较来自世界各地的卖家的域名和 IT 服务价格

如何使用函数 dataframe between_time//

我正在尝试使用这个功能
between_time

. 我格式化了一个字符串类型 time 在 datetime


dataset['TimeStamp'] = pd.to_datetime/dataset['TimeStamp'],format/


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


start = datetime.time/9,40,0/

end = datetime.time/10,00,0/


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


这是我得到的错误:


TypeError: Index must be DatetimeIndex


请问我如何解决它? 谢
已邀请:

小姐请别说爱

赞同来自:

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


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/


结果:


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


</class></class>

要回复问题请先登录注册