JP.
JP.

Reputation: 5606

Pandas Dataframe - Lookup Error

I am attempting to lookup a row in a pandas (version 0.14.1) dataframe using a date and stock ticker combination and am receiving a strange error.

My pandas dataframe that looks like this:

                         AAPL     IBM    GOOG    XOM       Date
2011-01-10 16:00:00  340.99  143.41  614.21  72.02 2011-01-10
2011-01-11 16:00:00  340.18  143.06  616.01  72.56 2011-01-11
2011-01-12 16:00:00  342.95  144.82  616.87  73.41 2011-01-12
2011-01-13 16:00:00  344.20  144.55  616.69  73.54 2011-01-13
2011-01-14 16:00:00  346.99  145.70  624.18  74.62 2011-01-14
2011-01-18 16:00:00  339.19  146.33  639.63  75.45 2011-01-18
2011-01-19 16:00:00  337.39  151.22  631.75  75.00 2011-01-19

When I attempt to do a lookup using a date/string combination I receive the following error:

>>> df_data.lookup(date,ticker)
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2820, in run_code
    exec code_obj in self.user_global_ns, self.user_ns
  File "<ipython-input-2-31ab981e2184>", line 1, in <module>
    df_data.lookup(date,ticker)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 2207, in lookup
    n = len(row_labels)
TypeError: object of type 'datetime.datetime' has no len()

From what I can see in the pandas documentation, this should work and my date variable is a regular date time

>>> date
Out[5]: datetime.datetime(2011, 1, 10, 16, 0)

Am I doing something obviously incorrect?

Upvotes: 1

Views: 653

Answers (1)

unutbu
unutbu

Reputation: 879411

df.lookup expects 2 array-likes (instead of scalars) as arguments:

In [25]: df.lookup(row_labels=[DT.datetime(2011,1,10,16,0)], col_labels=['AAPL'])
Out[25]: array([ 340.99])

If you only want to look up one value, use df.get_value instead:

In [30]: df.get_value(DT.datetime(2011,1,10,16,0), 'AAPL')
Out[30]: 340.99000000000001

Upvotes: 4

Related Questions