ValueError: Tz-aware datetime.datetime cannot be converted to datetime64 unless utc=True

1,986 views
Skip to first unread message

Snorre Hammerø Moen

unread,
Jan 2, 2016, 6:00:20 PM1/2/16
to Zipline Python Opensource Backtester

Getting an error msg when trying to run zipline in notebook, adding the add_history method. Looks like som error regarding the datetime? 
Any one know how to get by this?

Cheers


My code:

from zipline.api import (
    add_history,
    history,
    order_target,
    record,
    symbol,
    order,
)

import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('whitegrid')
%matplotlib inline

import pandas as pd 
import numpy as np

import pytz
from datetime import datetime
from zipline.algorithm import TradingAlgorithm
from zipline.utils.factory import load_bars_from_yahoo

# Load data manually from Yahoo! finance
start = datetime(2011, 1, 1, 0, 0, 0, 0, pytz.utc)
end = datetime(2012, 1, 1, 0, 0, 0, 0, pytz.utc)
data = load_bars_from_yahoo(stocks=['SPY'], start=start,
                            end=end)

# Define algorithm
def initialize(context):
    add_history(11, '1d', 'price')
    
    context.i = 0

def handle_data(context, data):
    
# Skip first periode to get the full window
    context.i += 1
    if context.i < 11:
        return
    
    order(symbol('SPY'), 10)  

    price_history = history(11, '1d', 'price')
    
     # Save values for later inspection
    record(SPY=data[symbol('SPY')].price)


# Create algorithm object passing in initialize and
# handle_data functions
algo_obj = TradingAlgorithm(initialize=initialize,
                            handle_data=handle_data)

# Run algorithm
perf_manual = algo_obj.run(data)

ValueError                                Traceback (most recent call last)
<ipython-input-31-6e48b596206f> in <module>()
      5 
      6 # Run algorithm
----> 7 perf_manual = algo_obj.run(data)

C:\Users\Eier\Anaconda\lib\site-packages\zipline\algorithm.pyc in run(self, source, overwrite_sim_params, benchmark_return_source)
    396                 self.history_specs,
    397                 self.sim_params.sids,
--> 398                 self.sim_params.first_open)
    399 
    400         # Create transforms by wrapping them into StatefulTransforms

C:\Users\Eier\Anaconda\lib\site-packages\zipline\history\history_container.pyc in __init__(self, history_specs, initial_sids, initial_dt)
    169         # cached since mid-stream creation of containing data frames on every
    170         # bar is expensive.
--> 171         self.create_return_frames(initial_dt)
    172 
    173         # Helps prop up the prior day panel against having a nan, when the data

C:\Users\Eier\Anaconda\lib\site-packages\zipline\history\history_container.pyc in create_return_frames(self, algo_dt)
    262         self.return_frames = {}
    263         for spec_key, history_spec in iteritems(self.history_specs):
--> 264             index = pd.to_datetime(index_at_dt(history_spec, algo_dt))
    265             frame = pd.DataFrame(
    266                 index=index,

C:\Users\Eier\Anaconda\lib\site-packages\pandas\util\decorators.pyc in wrapper(*args, **kwargs)
     87                 else:
     88                     kwargs[new_arg_name] = new_arg_value
---> 89             return func(*args, **kwargs)
     90         return wrapper
     91     return _deprecate_kwarg

C:\Users\Eier\Anaconda\lib\site-packages\pandas\tseries\tools.pyc in to_datetime(arg, errors, dayfirst, yearfirst, utc, box, format, exact, coerce, unit, infer_datetime_format)
    274     return _to_datetime(arg, errors=errors, dayfirst=dayfirst, yearfirst=yearfirst,
    275                         utc=utc, box=box, format=format, exact=exact,
--> 276                         unit=unit, infer_datetime_format=infer_datetime_format)
    277 
    278 

C:\Users\Eier\Anaconda\lib\site-packages\pandas\tseries\tools.pyc in _to_datetime(arg, errors, dayfirst, yearfirst, utc, box, format, exact, unit, freq, infer_datetime_format)
    393         return _convert_listlike(arg, box, format, name=arg.name)
    394     elif com.is_list_like(arg):
--> 395         return _convert_listlike(arg, box, format)
    396 
    397     return _convert_listlike(np.array([ arg ]), box, format)[0]

C:\Users\Eier\Anaconda\lib\site-packages\pandas\tseries\tools.pyc in _convert_listlike(arg, box, format, name)
    381                 return DatetimeIndex._simple_new(values, name=name, tz=tz)
    382             except (ValueError, TypeError):
--> 383                 raise e
    384 
    385     if arg is None:

ValueError: Tz-aware datetime.datetime cannot be converted to datetime64 unless utc=True

Joe Jevnik

unread,
Jan 5, 2016, 3:29:58 PM1/5/16
to Zipline Python Opensource Backtester
Hi, sorry about the trouble. I am unable to reproduce the error you are seeing when I run the code on master. Which version of zipline and pandas are you using? This might be a problem with a newer version of pandas.

trineary

unread,
Feb 1, 2016, 2:23:01 PM2/1/16
to Zipline Python Opensource Backtester
I ran into this problem as well.  I had pandas version 0.14.1 installed and reverted back to 0.13.1.  This reversion caused the error to disappear for me.  Hope that helps.

trineary

unread,
Feb 1, 2016, 2:23:24 PM2/1/16
to Zipline Python Opensource Backtester
I ran into this problem as well.  I had pandas version 0.14.1 installed and reverted back to 0.13.1.  This reversion caused the error to disappear for me.  Hope that helps.


On Tuesday, January 5, 2016 at 1:29:58 PM UTC-7, Joe Jevnik wrote:

Joe Jevnik

unread,
Feb 2, 2016, 5:45:54 PM2/2/16
to Zipline Python Opensource Backtester
Hey, sorry about the issue. What version of zipline are you using? Also, zipline currently uses pandas 0.16.1. I would try upgrading your packages to the versions specified in the etc/requirements.txt file to see if that resolves the issue.


On Saturday, January 2, 2016 at 6:00:20 PM UTC-5, Snorre Hammerø Moen wrote:

Snorre Hammerø Moen

unread,
Mar 3, 2016, 2:57:31 PM3/3/16
to Zipline Python Opensource Backtester
Thanks for answering,

I have zipline==0.7.0 and pandas==0.17.1. Installed pandas==0.16.1. It works very well now.

problem solved! :)
Reply all
Reply to author
Forward
0 new messages