gpt4 book ai didi

python - 如何使用 dataframe between_time() 函数

转载 作者:太空狗 更新时间:2023-10-29 22:30:23 25 4
gpt4 key购买 nike

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

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

关于python - 如何使用 dataframe between_time() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24576786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com