Pandas数据框中值的向量化查找

30

我有两个熊猫数据框,一个名为订单,另一个名为每日价格daily_prices如下:

              AAPL    GOOG     IBM    XOM
2011-01-10  339.44  614.21  142.78  71.57
2011-01-13  342.64  616.69  143.92  73.08
2011-01-26  340.82  616.50  155.74  75.89
2011-02-02  341.29  612.00  157.93  79.46
2011-02-10  351.42  616.44  159.32  79.68
2011-03-03  356.40  609.56  158.73  82.19
2011-05-03  345.14  533.89  167.84  82.00
2011-06-03  340.42  523.08  160.97  78.19
2011-06-10  323.03  509.51  159.14  76.84
2011-08-01  393.26  606.77  176.28  76.67
2011-12-20  392.46  630.37  184.14  79.97

orders如下:

           direction  size ticker  prices
2011-01-10       Buy  1500   AAPL  339.44
2011-01-13      Sell  1500   AAPL  342.64
2011-01-13       Buy  4000    IBM  143.92
2011-01-26       Buy  1000   GOOG  616.50
2011-02-02      Sell  4000    XOM   79.46
2011-02-10       Buy  4000    XOM   79.68
2011-03-03      Sell  1000   GOOG  609.56
2011-03-03      Sell  2200    IBM  158.73
2011-06-03      Sell  3300    IBM  160.97
2011-05-03       Buy  1500    IBM  167.84
2011-06-10       Buy  1200   AAPL  323.03
2011-08-01       Buy    55   GOOG  606.77
2011-08-01      Sell    55   GOOG  606.77
2011-12-20      Sell  1200   AAPL  392.46

两个数据帧的索引都是datetime.dateorders数据帧中的prices列是通过使用列表推导式循环遍历所有订单并在daily_prices数据帧中查找特定日期的特定股票,然后将该列表作为列添加到orders数据帧中的。我想使用数组操作而不是循环来完成这个操作。可以吗?我尝试使用:

daily_prices.ix[dates, tickers] 

但是这将返回两个列表的笛卡尔积矩阵。我希望它返回指定日期和股票代码的价格列向量。

1个回答

56

使用我们的 lookup 功能,它专门为此目的而设计:

In [17]: prices
Out[17]: 
              AAPL    GOOG     IBM    XOM
2011-01-10  339.44  614.21  142.78  71.57
2011-01-13  342.64  616.69  143.92  73.08
2011-01-26  340.82  616.50  155.74  75.89
2011-02-02  341.29  612.00  157.93  79.46
2011-02-10  351.42  616.44  159.32  79.68
2011-03-03  356.40  609.56  158.73  82.19
2011-05-03  345.14  533.89  167.84  82.00
2011-06-03  340.42  523.08  160.97  78.19
2011-06-10  323.03  509.51  159.14  76.84
2011-08-01  393.26  606.77  176.28  76.67
2011-12-20  392.46  630.37  184.14  79.97

In [18]: orders
Out[18]: 
                  Date direction  size ticker  prices
0  2011-01-10 00:00:00       Buy  1500   AAPL  339.44
1  2011-01-13 00:00:00      Sell  1500   AAPL  342.64
2  2011-01-13 00:00:00       Buy  4000    IBM  143.92
3  2011-01-26 00:00:00       Buy  1000   GOOG  616.50
4  2011-02-02 00:00:00      Sell  4000    XOM   79.46
5  2011-02-10 00:00:00       Buy  4000    XOM   79.68
6  2011-03-03 00:00:00      Sell  1000   GOOG  609.56
7  2011-03-03 00:00:00      Sell  2200    IBM  158.73
8  2011-06-03 00:00:00      Sell  3300    IBM  160.97
9  2011-05-03 00:00:00       Buy  1500    IBM  167.84
10 2011-06-10 00:00:00       Buy  1200   AAPL  323.03
11 2011-08-01 00:00:00       Buy    55   GOOG  606.77
12 2011-08-01 00:00:00      Sell    55   GOOG  606.77
13 2011-12-20 00:00:00      Sell  1200   AAPL  392.46

In [19]: prices.lookup(orders.Date, orders.ticker)
Out[19]: 
array([ 339.44,  342.64,  143.92,  616.5 ,   79.46,   79.68,  609.56,
        158.73,  160.97,  167.84,  323.03,  606.77,  606.77,  392.46])

5
我曾经尝试各种花哨的方法来自己做这件事,我应该知道你已经实现了它。感谢你这个很棒的包,Wes。让生活变得更加轻松。迫不及待地想看看你接下来会推出什么。 - luckyfool
当我将DateTime用作ordersprices数据框的索引时,使用略有不同的代码myval = prices.lookup(order[0], order[1]),我会得到“TypeError: object of type 'datetime.datetime' has no len()”。其中order来自于for order in orders。所以在我的情况下,order是1d而不是像上面(orders)的2d。这是错误的用法还是如何修复它?(我想从具有完全相同信息的价格数据框中获取单个日期和股票代码(来自订单数据框)的匹配条目。) - Andreas Reiff
2
我不确定这里是否会被注意到,但在这里尝试是有意义的:我想做一些类似的事情,但我需要将一系列值与按日期索引的系列匹配到一个按日期时间索引的数据框中。我得到了“series object has no attribute lookup.” 的错误提示。所以像 df['d'] = df.index.date -> df['x'] = ts.lookup(df.d) 这样的东西。 - M T

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接