Still a bit new to Pandas so I am trying to follow this tutorial here: https://www.learndatasci.com/tutorials/python-finance-part-yahoo-finance-api-pandas-matplotlib/
However, it seems a bit dated and I had to convert the data feed to pull from Tiingo instead. Otherwise trying to follow the tutorial as is and I run into an exception every time on the reindex call. Here's the exception:
...site-packages\pandas\core\indexes\multi.py", line 483, in from_tuples
arrays = list(lib.tuples_to_object_array(tuples).T)
TypeError: Argument 'tuples' has incorrect type (expected numpy.ndarray, got DatetimeArray)
Here's my version of the code.
import pandas as pd
import pandas_datareader as pdr
start_date = '2019-8-27'
end_date = '2019-8-30'
panel_data = pdr.get_data_tiingo('GLD', start=start_date, end=end_date, api_key='[REDACTED]')
close = panel_data['close']
all_weekdays = pd.date_range(start=start_date, end=end_date, freq='B')
close = close.reindex(all_weekdays)
The tutorial makes it seem like everything up to this point should just work, but I get the TypeError exception on that reindex()
call. Why?
I did try throwing a .to_numpy()
after all_weekdays
to satisfy the exception message but it still gives me the same exception including saying that it's a DatetimeArray.
Debugging so far makes me think that it's having issues because the close
series axes is set to a MultiIndex containing both symbol and date columns as the print output shows here.
[MultiIndex([('GLD', '2019-08-27 00:00:00+00:00'),
('GLD', '2019-08-28 00:00:00+00:00'),
('GLD', '2019-08-29 00:00:00+00:00'),
('GLD', '2019-08-30 00:00:00+00:00')],
names=['symbol', 'date'])]
This might make it have problems if it's reindexing against a simple list of dates in the form of the DatetimeIndex that is all_weekdays
.
DatetimeIndex(['2019-08-27', '2019-08-28', '2019-08-29', '2019-08-30'], dtype='datetime64[ns]', freq='B')
Do I need to coerce one index to match the other before reindexing? If so, how would I go about doing this? If not, what's an equivalent workaround to unblock me in this tutorial?