Hi, I'm stuck in chapter Chapter 12 - Momentum of the book. Tried everything. I got the following error both in Your fixed version and in the original code from the book. Can You help?
1997-01-02 - Last Month Result: 0.00%
TypeError Traceback (most recent call last)
c:\users...\appdata\local\programs\python\python38\lib\site-packages\pandas\core\arrays\datetimelike.py in _validate_comparison_value(self, other)
444 try:
--> 445 self._check_compatible_with(other)
446 except TypeError as err:
c:\users...\appdata\local\programs\python\python38\lib\site-packages\pandas\core\arrays\datetimes.py in _check_compatible_with(self, other, setitem)
474 return
--> 475 self._assert_tzawareness_compat(other)
476 if setitem:
c:\users...\appdata\local\programs\python\python38\lib\site-packages\pandas\core\arrays\datetimes.py in _assert_tzawareness_compat(self, other)
651 if other_tz is not None:
--> 652 raise TypeError(
653 "Cannot compare tz-naive and tz-aware datetime-like objects."
TypeError: Cannot compare tz-naive and tz-aware datetime-like objects.
The above exception was the direct cause of the following exception:
InvalidComparison Traceback (most recent call last)
c:\users...\appdata\local\programs\python\python38\lib\site-packages\pandas\core\arrays\datetimelike.py in _cmp_method(self, other, op)
931 try:
--> 932 other = self._validate_comparison_value(other)
933 except InvalidComparison:
c:\users...\appdata\local\programs\python\python38\lib\site-packages\pandas\core\arrays\datetimelike.py in _validate_comparison_value(self, other)
447 # e.g. tzawareness mismatch
--> 448 raise InvalidComparison(other) from err
449
InvalidComparison: 1997-01-02 21:00:00+00:00
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
in
c:\users...\appdata\local\programs\python\python38\lib\site-packages\zipline\utils\run_algo.py in run_algorithm(start, end, initialize, capital_base, handle_data, before_trading_start, analyze, data_frequency, bundle, bundle_timestamp, trading_calendar, metrics_set, benchmark_returns, default_extension, extensions, strict_extensions, environ, custom_loader, blotter)
395 benchmark_spec = BenchmarkSpec.from_returns(benchmark_returns)
396
--> 397 return _run(
398 handle_data=handle_data,
399 initialize=initialize,
c:\users...\appdata\local\programs\python\python38\lib\site-packages\zipline\utils\run_algo.py in _run(handle_data, initialize, before_trading_start, analyze, algofile, algotext, defines, data_frequency, capital_base, bundle, bundle_timestamp, start, end, output, trading_calendar, print_algo, metrics_set, local_namespace, environ, blotter, custom_loader, benchmark_spec)
198
199 try:
--> 200 perf = TradingAlgorithm(
201 namespace=namespace,
202 data_portal=data,
c:\users...\appdata\local\programs\python\python38\lib\site-packages\zipline\algorithm.py in run(self, data_portal)
622 try:
623 perfs = []
--> 624 for perf in self.get_generator():
625 perfs.append(perf)
626
c:\users...\appdata\local\programs\python\python38\lib\site-packages\zipline\gens\tradesimulation.py in transform(self)
226 for dt, action in self.clock:
227 if action == BAR:
--> 228 for capital_change_packet in every_bar(dt):
229 yield capital_change_packet
230 elif action == SESSION_START:
c:\users...\appdata\local\programs\python\python38\lib\site-packages\zipline\gens\tradesimulation.py in every_bar(dt_to_use, current_data, handle_data)
141 metrics_tracker.process_commission(commission)
142
--> 143 handle_data(algo, current_data, dt_to_use)
144
145 # grab any new orders from the blotter, then clear the list.
c:\users...\appdata\local\programs\python\python38\lib\site-packages\zipline\utils\events.py in handle_data(self, context, data, dt)
204 with self._create_context(data):
205 for event in self._events:
--> 206 event.handle_data(
207 context,
208 data,
c:\users...\appdata\local\programs\python\python38\lib\site-packages\zipline\utils\events.py in handle_data(self, context, data, dt)
227 """
228 if self.rule.should_trigger(dt):
--> 229 self.callback(context, data)
230
231
~\AppData\Local\Temp\tmpmim2g0sm/ipykernel_14288/3887532712.py in rebalance(context, data)
9
10 # Second, get the index makeup for all days prior to today.
---> 11 all_prior = context.index_members.loc[context.index_members.index < today]
12
13 # Now let's snag the first column of the last, i.e. latest, entry.
c:\users...\appdata\local\programs\python\python38\lib\site-packages\pandas\core\indexes\extension.py in wrapper(self, other)
132
133 op = getattr(self._data, opname)
--> 134 return op(other)
135
136 wrapper.name = opname
c:\users...\appdata\local\programs\python\python38\lib\site-packages\pandas\core\ops\common.py in new_method(self, other)
63 other = item_from_zerodim(other)
64
---> 65 return method(self, other)
66
67 return new_method
c:\users...\appdata\local\programs\python\python38\lib\site-packages\pandas\core\arraylike.py in lt(self, other)
35 @unpack_zerodim_and_defer("lt")
36 def lt(self, other):
---> 37 return self._cmp_method(other, operator.lt)
38
39 @unpack_zerodim_and_defer("le")
c:\users...\appdata\local\programs\python\python38\lib\site-packages\pandas\core\arrays\datetimelike.py in _cmp_method(self, other, op)
932 other = self._validate_comparison_value(other)
933 except InvalidComparison:
--> 934 return invalid_comparison(self, other, op)
935
936 dtype = getattr(other, "dtype", None)
c:\users...\appdata\local\programs\python\python38\lib\site-packages\pandas\core\ops\invalid.py in invalid_comparison(left, right, op)
32 else:
33 typ = type(right).name
---> 34 raise TypeError(f"Invalid comparison between dtype={left.dtype} and {typ}")
35 return res_values
36
TypeError: Invalid comparison between dtype=datetime64[ns] and Timestamp