我尝试使用以下几行从IOCommandQueue
获取单个值:
pd.DataFrame
如何告诉pandas将返回值限制为end = datetime.datetime.strptime('2016-03-01', '%Y-%m-%d')
timeranges = [(end - datetime.timedelta(30)),
(end - datetime.timedelta(60)),
(end - datetime.timedelta(365))]
nav_df = data[(data['Symbol'] == 'account') & (data['Type'] == 'nav')
& (data['Date'] >= date)]
中评估为True
的第一个日期?
示例数据:
答案 0 :(得分:1)
IIUC您可以使用Datetime
作为字符串end
,如:
import pandas as pd
data = pd.read_csv('http://pastebin.com/raw/1pRatDts',
sep="\t",
header=None,
parse_dates = [1],
names=['Symbol','Date','Type','Values'])
print data.head()
Symbol Date Type Values
0 account 2012-01-02 nav 460515.3849
1 account 2012-01-03 nav 459722.4779
2 account 2012-01-04 nav 460089.9382
3 account 2012-01-05 nav 460961.5919
4 account 2012-01-06 nav 461316.4826
end = '2016-03-01'
nav_df = data[(data['Symbol'] == 'account') &
(data['Type'] == 'nav') &
(data['Date'] >= end)]
print nav_df
Symbol Date Type Values
1083 account 2016-03-01 nav 501594.4239
1084 account 2016-03-02 nav 500964.1165
1085 account 2016-03-03 nav 500921.3288
1086 account 2016-03-04 nav 500000.0000
第一行感谢John Galt:
print nav_df.head(1)
Symbol Date Type Values
1083 account 2016-03-01 nav 501594.4239
或者如果您Datetimeindex
使用first
:
nav_df = nav_df.set_index('Date')
print nav_df.first('D')
Symbol Type Values
Date
2016-03-01 account nav 501594.4239
答案 1 :(得分:1)
从@jezrael基地起飞,
您可以使用(data['Date'] >= end).diff().fillna(True)
将返回值限制为中评估为True的第一个日期。
In [215]: data[(data['Symbol'] == 'account') &
.....: (data['Type'] == 'nav') &
.....: (data['Date'] >= end).diff().fillna(True)]
Out[215]:
Symbol Date Type Values
1083 account 2016-03-01 nav 501594.4239