我正在尝试使用min()
找到两点之间的between_time
值。我创建了两列,我想用它作为开始和结束时间来查找最小值并将输出添加到新列:
这是df的剪辑:
df[['Date_Time','d1_idx_last','Low']]
Date_Time d1_idx_last Low
Timestamp
2015-09-01 09:30:00.000 2015-09-01 09:30:00.000 2015-09-01 16:14:51.000 1887.750
2015-09-01 09:30:01.000 2015-09-01 09:30:01.000 2015-09-01 16:14:51.000 1888.250
2015-09-01 09:30:01.200 2015-09-01 09:30:01.200 2015-09-01 16:14:51.000 1888.000
2015-09-01 09:30:10.100 2015-09-01 09:30:10.100 2015-09-01 16:14:51.000 1889.250
2015-09-01 09:30:11.100 2015-09-01 09:30:11.100 2015-09-01 16:14:51.000 1889.500
我正在尝试使用此代码:
df.Low.between_time(df.Date_Time, df.d1_idx_last, include_start=True, include_end=True)
并收到此错误:
Cannot convert arg [1441099800000000000 1441099801000000000 1441099801200000000 ...,
1470924200100000000 1470924369230000000 1470924793157000000] to a time
列'Date_Time'
& 'd1_idx_last'
都是pandas.tslib.Timestamp
类型。
更新以澄清:
所以,如果我们看一下它显示的第一行
'Date_Time' 2015-09-01 09:30:00.000
'd1_idx_last'2015-09-01 16:14:51.000
在此行上'Date_Time'
&之间的时间'd1_idx_last'
捕捉整个交易日(09:30-16:15),我希望这两点之间的时间较短。
在这一天,市场跌至1863.500,这将是(09:30-16:15)的最小值。
df[['Low']]['2015-09-01'].min()
Low 1863.500
dtype: float64
如果低点1863.500在13:00出现,则此后滚动低点会更高。
我想要一个名为df['subset_low']
的新列,用于检查'Date_Time'
&每行'd1_idx_last'
并找到此期间的低点并将其添加到df ['subset_low']。它正在检查当前时间和当天的最后一点,并显示这段时间之间的低点。
答案 0 :(得分:2)
更新:使用丑陋的方法 - apply(..., axis=1)
:
In [170]: df['subset_low'] = df.apply(lambda r: df.query('@r.Date_Time <= index <= @r.d1_idx_last').Low.min(), axis=1)
In [171]: df
Out[171]:
Date_Time d1_idx_last Low subset_low
idx
2015-09-01 09:30:00.000 2015-09-01 09:30:00.000 2015-09-01 16:14:51 2 1
2015-09-01 09:30:01.000 2015-09-01 09:30:01.000 2015-09-01 16:14:51 1 1
2015-09-01 09:30:01.200 2015-09-01 09:30:01.200 2015-09-01 16:14:51 3 3
2015-09-01 09:30:10.100 2015-09-01 09:30:10.100 2015-09-01 16:14:51 4 3
2015-09-01 09:30:11.100 2015-09-01 09:30:11.100 2015-09-01 16:14:51 3 3
OLD回答:
由于@JonClements已经said between_time()方法需要前两个参数的标量值 - (start_time
,end_time
)并且它只检查时间部分。< / p>
演示:
In [72]: df.between_time('09:30:10','09:30:15')
Out[72]:
Date_Time d1_idx_last Low
idx
2015-09-01 09:30:10.100 2015-09-01 09:30:10.100 2015-09-01 16:14:51 1889.25
2015-09-01 09:30:11.100 2015-09-01 09:30:11.100 2015-09-01 16:14:51 1889.50
您可以使用query()方法
In [70]: df.query('Date_Time <= index <= d1_idx_last')
Out[70]:
Date_Time d1_idx_last Low
idx
2015-09-01 09:30:00.000 2015-09-01 09:30:00.000 2015-09-01 16:14:51 1887.75
2015-09-01 09:30:01.000 2015-09-01 09:30:01.000 2015-09-01 16:14:51 1888.25
2015-09-01 09:30:01.200 2015-09-01 09:30:01.200 2015-09-01 16:14:51 1888.00
2015-09-01 09:30:10.100 2015-09-01 09:30:10.100 2015-09-01 16:14:51 1889.25
2015-09-01 09:30:11.100 2015-09-01 09:30:11.100 2015-09-01 16:14:51 1889.50
如何使用min()
在df.Low
和Date_Time
之间获得d1_idx_last
df.query
个?{/ p>
In [74]: df.query('Date_Time <= index <= d1_idx_last').Low.min()
Out[74]: 1887.75