python - Pandas Timestamp - 无法将 arg 转换为时间错误

标签 python pandas

我正在尝试使用 Between_time 查找两点之间的 min() 值。我创建了两列,我想将它们用作开始和结束时间,以查找最小值并将输出添加到新列:

这是 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']。它正在检查当前时间和当天的最后一点,并显示该时间之间的最低价。

@Maxu 在 Low 和(所需的)subset_low 列中使用虚假数据的另一个示例: enter image description here

最佳答案

更新:使用丑陋的方法 - 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

旧答案:

已经作为@JonClements said between_time()方法需要前两个参数的标量值( start_timeend_time ),并且仅检查时间部分。

演示:

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_Timed1_idx_last使用df.query

In [74]: df.query('Date_Time <= index <= d1_idx_last').Low.min()
Out[74]: 1887.75

关于python - Pandas Timestamp - 无法将 arg 转换为时间错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38944599/

相关文章:

python - 神经网络MPC

python - 如何在遍历每个单词后用换行符返回单词中的字母?

python - Pandas 数据框将行值 reshape 为新列(矩阵类型格式)

python - 如何使用来自多列的参数调用 pandas.rolling.apply?

python - 如果识别出特定元素,则乘以 DataFrame 中的值

python - 如何在python for循环中用变量控制增量

python - pandas如何根据df中的其他 bool 列创建 bool 列

python - 我怎样才能删除变量的这一部分?

python - Beautifulsoup - 收集 href 链接并创建链接列表

python-2.7 - pandas 数据帧到 oracle - NotImplementedError