我有一个看起来像这样的pandas DataFrame:
Symbol Date Time Bid Price Bid Exchange Bid Size Ask Price Ask Exchange Ask Size
0 ABC 2014-03-03 09:30:00.033000 43.16 Z 1 43.69 P 3
1 ABC 2014-03-03 09:30:00.038000 43.17 P 3 43.69 P 3
2 ABC 2014-03-03 09:30:00.039000 43.17 P 4 43.69 P 3
3 ABC 2014-03-03 09:30:00.151000 43.3 P 6 43.69 P 3
4 ABC 2014-03-03 09:30:00.151000 43.3 P 6 43.42 P 4
我想每分钟提取一次数据并进行一些计算。为此,我会反复地向start_time
添加1分钟,并将其命名为end_time
,然后在start_time
和end_time
之间选择数据并进行分析;我的代码如下:
import numpy as np
import pandas as pd
import datetime
from datetime import date, time, timedelta
df = pd.read_csv('some_data_file.csv'))
#Converting strings to date and time
df['Date'] = pd.to_datetime(df['Date'])
df['Time'] = pd.to_datetime(df['Time'])
df['Date'] = df['Date'].dt.date
df['Time'] = df['Time'].dt.time
start_date = datetime.date(2014, 3, 3)
end_date = datetime.date(2014, 3, 4)
day_i = df.loc[df['Date']==start_date]
start_time = day_i['Time'][0]
end_time = start_time + timedelta.time(minutes=1)
interval_i = day_i.loc[(day_i['Time'] >= start_time) & (day_i['Time'] <= end_time)]
start_time
的格式为datetime.time(9, 30, 0, 33000)
。
当我跑步时:
end_time = start_time + timedelta.time(minutes=1)
我收到此错误:
AttributeError: type object 'datetime.timedelta' has no attribute 'time'
答案 0 :(得分:1)
首先请确保开始时间和结束时间的类型为时间,而不是日期时间 >。如果需要,转换为时间类型。
type(start_time)
示例:
start_time = (9, 30, 0, 0)
end_time = start_time + timedelta(seconds=60)
您应该获得:end_time = (9, 30, 1, 0)
您还可以尝试 pandas between_time()
myrows = df['Time'].between_time(start_time, end_time)
DataFrame.between_time(开始时间,结束时间,include_start = True,include_end = True)[源代码] 选择一天中特定时间(例如9:00-9:30 AM)之间的值。