Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

python - how to use dataframe between_time() function

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

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)

This is the error I get:

TypeError: Index must be DatetimeIndex

Please how can I fix it. Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 "
DataFrame:
", df 

print '
Index:', type(df.index)

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

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

print "
DataFrame:
", df 

print '
Index:', type(df.index)

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

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

print "
Result:
", 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...