Search code examples
pythonpandascomputational-finance

Panda runtime warning Cannot compare type 'Timestamp' with type 'str', sort order is undefined for incomparable objects


I am currently working on homework 2 for the coursera computational finance.

While executing this line:

ep.eventprofiler(df_events, d_data, i_lookback=20, i_lookforward=20,
            s_filename=report_filename, b_market_neutral=True, b_errorbars=True,
            s_market_sym='SPY')

I get the error:

anaconda/lib/python2.7/site-packages/pandas/indexes/base.py:2397: RuntimeWarning: Cannot compare type 'Timestamp' with type 'str', sort order is undefined for incomparable objects
  return this.join(other, how=how, return_indexers=return_indexers)

Which creates the pdf file, shows the number of events occurred but not actually draw the events. I am not sure why this occurs. I am using pandas 0.18.0

Any ideas? I appreciate the help.

df_events.dtypes sample:

ALTR    float64
ALXN    float64
AMAT    float64
AMD     float64
AMGN    float64
AMP     float64
AMT     float64
         ...
WDC     float64
WEC     float64
WFC     float64
WFM     float64
WHR     float64
WIN     float64
WLP     float64
WM      float64
WMB     float64
WMT     float64
XLNX    float64
XOM     float64
XRAY    float64
XRX     float64
XYL     float64
YHOO    float64
YUM     float64
ZION    float64
ZMH     float64
SPY     float64
dtype: object

Here is the d_data.dtypes log sample:

           YHOO    YUM   ZION    ZMH     SPY
2008-01-02 16:00:00  23.72  37.88  45.29  66.29  144.93
2008-01-03 16:00:00  23.84  37.35  44.38  66.36  144.86
2008-01-04 16:00:00  23.16  36.82  42.40  66.50  141.31
2008-01-07 16:00:00  23.18  37.68  43.28  68.66  141.19

I get

 d_data.dtypes
*** AttributeError: 'dict' object has no attribute 'dtypes'

when I try to print out the d_data dtypes.


Solution

  • the trouble is caused by the line:

    df_rets = df_rets - df_rets[s_market_sym]
    

    in these couple of lines:

    if b_market_neutral == True:
        df_rets = df_rets - df_rets[s_market_sym]
        del df_rets[s_market_sym]
        del df_events[s_market_sym]
    

    in eventprofiler(...) function. Quite frankly I think the line is a bug and it should be put as a comment, to say at least - as the logic behind escapes my understanding. Others are just fine.

    If you set the argument b_market_neutral to False, it will give you a nice graph, but that also takes into account the SPY market data when calculating the mean return. So the workaround, in order to use a "proper" logic when calculating mean values, would be to comment this line and recompile QSTK with this modification.

    Hope this helps.

    Regards, Daniel