Pandas' corrwith usage about SPY vs. ^GSPC - python

Pandas' corrwith work fine with SPY but not OK with ^GSPC.
Can anybody help me ? Thanks in advance.
import pandas.io.data as web
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import csv
Symbol = ['^GSPC','SPY','XLK','XLV','XLY','XLP','XLE','XLF','XLI','XLB','XLU']
STOCK = web.DataReader(name=Symbol,data_source='yahoo',start='2000-1-1',end='2009-3-1')
STOCK['PercentA'] = np.round(np.log(STOCK['Adj Close']/STOCK['Adj Close'].shift(1)),3)
### OK wiht SPY, but not OK wiht ^GSPC
STOCKCORR = np.round(STOCK['PercentA'].corrwith(STOCK['PercentA'].^GSPC),3)
StockNan = STOCKCORR[~STOCKCORR.isnull()]
StockOrder = StockNan.order().iloc[::-1]
StockRows = StockOrder.iloc[:20]
print StockRows
the error messages:
runfile('D:/###pg/yahoo_final_Chang/150830__Revised/test.py', wdir='D:/###pg/yahoo_final_Chang/150830__Revised')
Traceback (most recent call last):
File "", line 1, in
File "C:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 682, in runfile
execfile(filename, namespace)
File "C:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "D:/###pg/yahoo_final_Chang/150830__Revised/test.py", line 16
STOCKCORR = np.round(STOCK['PercentA'].corrwith(STOCK['PercentA'].^GSPC),3)
^
SyntaxError: invalid syntax

Related

Python "checknull() takes no keyword arguments" error

I have tried to research this error and I cannot seem to find an answer. I've never seen this and this code worked fine last month and now I receive this error with the same code. Please advise and thank you. I've included my code and the error message below. The program is designed to read data from a URL and ingest that data into a new dataframe. I do not understand the meaning of "checknull()".
import os
os.environ["PROJ_LIB"] = 'C:\\Users\\Yury\\anaconda3\\Library\\share'
from sys import exit
import netCDF4 as nc4
from netCDF4 import Dataset
import numpy as np
import matplotlib as m
import matplotlib.pyplot as plt
#from mpl_toolkits.basemap import Basemap, cm
import datetime
from datetime import datetime
import pandas as pd
import xarray as xr
import cartopy.crs as ccrs
import math
#import cdstoolbox as ct
import bottleneck as bn
from mpl_toolkits.basemap import Basemap
import cdsapi
from matplotlib.pyplot import figure
import cartopy.feature as cfeature
import time
import calendar
# -----------------------------------------------------------------------------------------------------------
#
# -----------------------------------------------------------------------------------------------------------
#setx ECCODES_DEFINITION_PATH "C:\\Users\\U321103\\Anaconda3\\envs\\Maps2\\Library\\share\\eccodes\\definitions"
# copy setx... in command prompt in C:\\Users\\U321103
#!/usr/bin/env python3
c = cdsapi.Client()
url = c.retrieve(
'reanalysis-era5-single-levels-monthly-means',
{
'product_type': 'monthly_averaged_reanalysis',
'format': 'grib',
'variable': ['Mean sea level pressure'],
'year': ['1992','1993','1994','1995','1996','1997','1998','1999','2000','2001','2002','2003','2004','2005','2006',
'2007','2008','2009','2010','2011','2012','2013','2014','2015','2016','2017','2018','2019','2020','2021','2022'],
'month': ['01','02','03','04','05','06','07','08','09','10','11','12'],
# 'month': '12',
# 'day': '01',
'time': ['00:00'],
'grid': [0.25, 0.25],
# GRID = 0.5 TO MATCH VORTEX ?
'area': [65.00, -140.00, 15.00, -53.00],
},
"C:\\Users\\U321103\\.spyder-py3\\ERA5_MAPPING\\mean_sea_level_pressure")
path = "C:\\Users\\U321103\\.spyder-py3\\ERA5_MAPPING\\mean_sea_level_pressure"
ds = xr.load_dataset(path, engine='cfgrib')
exit()
And the error --- >
runfile('//porfiler03.ar.local/gtdshare/GOALS_2022/WeatherTypes/ERA5_Get_MSLP_USA_WORKING_MONTHLY.py', wdir='//porfiler03.ar.local/gtdshare/GOALS_2022/WeatherTypes')
2023-01-16 18:09:51,830 INFO Welcome to the CDS
2023-01-16 18:09:51,831 INFO Sending request to https://cds.climate.copernicus.eu/api/v2/resources/reanalysis-era5-single-levels-monthly-means
2023-01-16 18:09:52,149 INFO Request is completed
2023-01-16 18:09:52,150 INFO Downloading https://download-0001-clone.copernicus-climate.eu/cache-compute-0001/cache/data0/adaptor.mars.internal-1673912965.1298344-26515-5-eb9a349c-be6c-4e0c-b3fb-79e0d436a411.grib to C:\Users\U321103\.spyder-py3\ERA5_MAPPING\mean_sea_level_pressure (49.9M)
2023-01-16 18:09:59,400 INFO Download rate 6.9M/s
2023-01-16 18:09:59,401 WARNING Ignoring index file 'C:\\Users\\U321103\\.spyder-py3\\ERA5_MAPPING\\mean_sea_level_pressure.923a8.idx' older than GRIB file
Traceback (most recent call last):
File "C:\Users\U321103\Anaconda3\envs\Maps2\lib\site-packages\xarray\conventions.py", line 523, in decode_cf_variables
new_vars[k] = decode_cf_variable(
File "C:\Users\U321103\Anaconda3\envs\Maps2\lib\site-packages\xarray\conventions.py", line 364, in decode_cf_variable
var = coder.decode(var, name=name)
File "C:\Users\U321103\Anaconda3\envs\Maps2\lib\site-packages\xarray\coding\variables.py", line 189, in decode
encoded_fill_values = {
File "C:\Users\U321103\Anaconda3\envs\Maps2\lib\site-packages\xarray\coding\variables.py", line 193, in <setcomp>
if not pd.isnull(fv)
File "C:\Users\U321103\Anaconda3\envs\Maps2\lib\site-packages\pandas\core\dtypes\missing.py", line 185, in isna
return _isna(obj)
File "C:\Users\U321103\Anaconda3\envs\Maps2\lib\site-packages\pandas\core\dtypes\missing.py", line 208, in _isna
return libmissing.checknull(obj, inf_as_na=inf_as_na)
TypeError: checknull() takes no keyword arguments
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\U321103\Anaconda3\envs\Maps2\lib\site-packages\spyder_kernels\py3compat.py", line 356, in compat_exec
exec(code, globals, locals)
File "\\porfiler03.ar.local\gtdshare\goals_2022\weathertypes\era5_get_mslp_usa_working_monthly.py", line 58, in <module>
ds = xr.load_dataset(path, engine='cfgrib')
File "C:\Users\U321103\Anaconda3\envs\Maps2\lib\site-packages\xarray\backends\api.py", line 258, in load_dataset
object.
File "C:\Users\U321103\Anaconda3\envs\Maps2\lib\site-packages\xarray\backends\api.py", line 545, in open_dataset
ds = _dataset_from_backend_dataset(
File "C:\Users\U321103\Anaconda3\envs\Maps2\lib\site-packages\xarray\backends\api.py", line 451, in maybe_decode_store
specified). If None (default), attempt to decode times to
File "C:\Users\U321103\Anaconda3\envs\Maps2\lib\site-packages\xarray\conventions.py", line 659, in decode_cf
vars, attrs, coord_names = decode_cf_variables(
File "C:\Users\U321103\Anaconda3\envs\Maps2\lib\site-packages\xarray\conventions.py", line 534, in decode_cf_variables
raise type(e)(f"Failed to decode variable {k!r}: {e}")
TypeError: Failed to decode variable 'number': checknull() takes no keyword arguments

Import errors in Numba

The code :::: Does anyone can help me with this ?
from numba import vectorize
#vectorize(['int64(int64, int64)'], target='cuda')
def add_ufunc(x, y):
return x + y
print('a+b:\n', add_ufunc(a, b))
print()
print('b_col + c:\n', add_ufunc(b_col, c))
The error ::::
Traceback (most recent call last): File "array.py", line 1, in
from numba import vectorize File "/usr/lib/python3.7/site-packages/numba/init.py", line 11, in
from . import config, errors, _runtests as runtests, types File "/usr/lib/python3.7/site-packages/numba/config.py", line 9, in
import multiprocessing File "/usr/lib/python3.7/multiprocessing/init.py", line 16, in
from . import context File "/usr/lib/python3.7/multiprocessing/context.py", line 6, in
from . import reduction File "/usr/lib/python3.7/multiprocessing/reduction.py", line 136, in
import array File "/home/felipe/cuda/array.py", line 1, in
from numba import vectorize ImportError: cannot import name 'vectorize' from 'numba'
(/usr/lib/python3.7/site-packages/numba/init.py)

PYOMO spreadsheet reading issue

We have a model that uses Dataportal of Pyomo to read parameter from several csv files. On a Windows laptop we are running into the following error while this is not replicable on another computer. Any ideas what might be missing in this setting?
Traceback (most recent call last):
File "", line 1, in
runfile('C:/Users/stianbac/OneDrive - NTNU/EMPIRE/EMPIRE in Pyomo/EMPIRE_Pyomo_version_4/Empire_draft4.py',
wdir='C:/Users/stianbac/OneDrive - NTNU/EMPIRE/EMPIRE in
Pyomo/EMPIRE_Pyomo_version_4')
File
"C:\Users\stianbac\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py",
line 710, in runfile
execfile(filename, namespace)
File
"C:\Users\stianbac\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py",
line 101, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/stianbac/OneDrive - NTNU/EMPIRE/EMPIRE in
Pyomo/EMPIRE_Pyomo_version_4/Empire_draft4.py", line 107, in
instance = model.create_instance(data)
File
"C:\Users\stianbac\AppData\Local\Continuum\anaconda3\lib\site-packages\pyomo\core\base\DataPortal.py",
line 138, in load
self.connect(**kwds)
File
"C:\Users\stianbac\AppData\Local\Continuum\anaconda3\lib\site-packages\pyomo\core\base\DataPortal.py",
line 98, in connect
self._data_manager.open()
File
"C:\Users\stianbac\AppData\Local\Continuum\anaconda3\lib\site-packages\pyomo\core\plugins\data\sheet.py",
line 54, in open
self.sheet = ExcelSpreadsheet(self.filename, ctype=self.ctype)
File
"C:\Users\stianbac\AppData\Local\Continuum\anaconda3\lib\site-packages\pyutilib\excel\spreadsheet.py",
line 79, in new
return ExcelSpreadsheet_win32com(*args, **kwds)
File
"C:\Users\stianbac\AppData\Local\Continuum\anaconda3\lib\site-packages\pyutilib\excel\spreadsheet_win32com.py",
line 59, in init
self.open(filename, worksheets, default_worksheet)
File
"C:\Users\stianbac\AppData\Local\Continuum\anaconda3\lib\site-packages\pyutilib\excel\spreadsheet_win32com.py",
line 80, in open
self._ws[wsid] = self.wb.Worksheets.Item(wsid)
File
"C:\Users\stianbac\AppData\Local\Continuum\anaconda3\lib\site-packages\win32com\client\dynamic.py",
line 516, in getattr
ret = self.oleobj.Invoke(retEntry.dispid,0,invoke_type,1)
com_error: (-2147418111, 'Call was rejected by callee.', None, None)
Here is the entry of the code:
from __future__ import division
from pyomo.environ import *
#from pyomo.core.expr import current as EXPR
#import numpy as np
import math
import csv
model = AbstractModel()
model.Nodes = Set()
model.Generators = Set() #g
...
data = DataPortal()
data.load(filename='Sets.xlsx',range='B1:B53',using='xlsx',format="set", set=model.Generators)
data.load(filename='Sets.xlsx',range='nodes',using='xlsx',format="set", set=model.Nodes)
...
instance = model.create_instance(data)
...

Getting error to extract data from coinmarketcap

import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
import time
import seaborn as sns
import matplotlib.pyplot as plt
import datetime
import numpy as np
df=pd.read_excel('currencynames.xlsx', sheet_name='Sheet1')
writer = pd.ExcelWriter('cryptodata.xlsx', engine='xlsxwriter')
currencies = df['Currency Name']
print(currencies[0])
for i in range(0,1465,1):
print("https://coinmarketcap.com/currencies/"+currencies[i]+"/historical-data/?start=20130428&end="+time.strftime("%Y%m%d"))
currency_market_info = pd.read_html("https://coinmarketcap.com/currencies/"+currencies[i].lower()+"/historical-data/?start=20130428&end="+time.strftime("%Y%m%d"))[0]
currency_market_info = currency_market_info.assign(Date=pd.to_datetime(currency_market_info['Date']))
currency_market_info.loc[currency_market_info['Volume']=="-",'Volume']=0
currency_market_info['Volume'] = currency_market_info['Volume'].astype('int64')
currency_market_info.head()
currency_market_info.to_excel(writer, sheet_name=currencies[i])
currency_market_info = 0
print("Done with "+currencies[i])
C:\Users\SAU\Anaconda3\lib\site-packages\pandas\core\ops.py:816:
FutureWarning: elementwise comparison failed; returning scalar
instead, but in the future will perform elementwise comparison
result = getattr(x, name)(y) Traceback (most recent call last):
File "", line 1, in
runfile('C:/Users/SAU/Desktop/deep learning/cryptodata/data.py', wdir='C:/Users/SAU/Desktop/deep learning/cryptodata')
File
"C:\Users\SAU\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py",
line 880, in runfile
execfile(filename, namespace)
File
"C:\Users\SAU\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py",
line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/SAU/Desktop/deep learning/cryptodata/data.py", line
19, in
currency_market_info.loc[currency_market_info['Volume']=="-",'Volume']=0
File "C:\Users\SAU\Anaconda3\lib\site-packages\pandas\core\ops.py",
line 879, in wrapper
res = na_op(values, other)
File "C:\Users\SAU\Anaconda3\lib\site-packages\pandas\core\ops.py",
line 818, in na_op
raise TypeError("invalid type comparison")
TypeError: invalid type comparison

Pyalgotrade - AttributeError: 'SequenceDatasSeries' object has no attribute 'getHighDataSeries'

I'm using pyalgotrade to write a trading algo. I got the error above and can't seem to fix it. I'm trying to use the "SLOW STOCHASTIC", any help on solving this error and getting the slow stochastic to work is greatly appreciated:
Error:
C:\Users\...\Desktop>python bobo.py
Traceback (most recent call last):
File "bobo.py", line 114, in <module>
main()
File "bobo.py", line 110, in main
run_strategy(10,inst,2,14,5,2,3)
File "bobo.py", line 102, in run_strategy
myStrategy = MyStrategy(feed, inst, smaPeriod,emaPeriod,rsiPeriod,fastk_period,slowk_period,slowd_period)
File "bobo.py", line 26, in __init__
self.__stoch = indicator.STOCH(self.__prices,fastk_period,slowk_period,slowd_period)
File "C:\Users\...\Anaconda2\lib\site-packages\pyalgotrade\talibext\indicator.py", line 803, in STOCH
ret = call_talib_with_hlc(barDs, count, talib.STOCH, fastk_period, slowk_period, slowk_matype, slowd_period, slowd_matype)
File "C:\Users\...\Anaconda2\lib\site-packages\pyalgotrade\talibext\indicator.py", line 93, in call_talib_with_hlc
high = bar_ds_high_to_numpy(barDs, count)
File "C:\Users\...\Anaconda2\lib\site-packages\pyalgotrade\talibext\indicator.py", line 45, in bar_ds_high_to_numpy
return value_ds_to_numpy(barDs.getHighDataSeries(), count)
AttributeError: 'SequenceDataSeries' object has no attribute 'getHighDataSeries'
Code:
from pyalgotrade.tools import yahoofinance
from pyalgotrade import strategy
from pyalgotrade.barfeed import yahoofeed
from pyalgotrade.technical import stoch
from pyalgotrade import dataseries
from pyalgotrade.technical import ma
from pyalgotrade import technical
from pyalgotrade.technical import highlow
from pyalgotrade import bar
from pyalgotrade.talibext import indicator
from pyalgotrade.technical import rsi
import numpy
import talib
class MyStrategy(strategy.BacktestingStrategy):
def __init__(self, feed,instrument,smaPeriod,emaPeriod,rsiPeriod,fastk_period,slowk_period,slowd_period):
strategy.BacktestingStrategy.__init__(self, feed, 1000) #change portfolio amount
self.__position = None
self.__instrument = instrument
self.setUseAdjustedValues(True)
self.__prices = feed[instrument].getPriceDataSeries()
self.__sma = ma.SMA(self.__prices, smaPeriod)
self.__ema = ma.EMA(self.__prices, emaPeriod)
self.__rsi = rsi.RSI(self.__prices, rsiPeriod)
self.__stoch = indicator.STOCH(self.__prices,fastk_period,slowk_period,slowd_period)
Now I'm getting the error:
Traceback (most recent call last):
File "bobo.py", line 103, in <module>
main()
File "bobo.py", line 99, in main
run_strategy(inst,10,250,14,5,5,5)
File "bobo.py", line 90, in run_strategy
myStrategy = MyStrategy(feed, inst, smaPeriod,emaPeriod,rsiPeriod,fastk_period,slowk_period,slowd_period)
File "bobo.py", line 28, in __init__
self.__stoch = indicator.STOCH(feed[instrument],fastk_period,slowk_period,slowd_period)
File "C:\Users\JDOG\Anaconda2\lib\site-packages\pyalgotrade\talibext\indicator.py", line 803, in STOCH
ret = call_talib_with_hlc(barDs, count, talib.STOCH, fastk_period, slowk_period, slowk_matype, slowd_period, slowd_matype)
File "C:\Users\JDOG\Anaconda2\lib\site-packages\pyalgotrade\talibext\indicator.py", line 105, in call_talib_with_hlc
return talibFunc(high, low, close, *args, **kwargs)
File "talib/func.pyx", line 9388, in talib.func.STOCH (talib\func.c:87125)
Exception: inputs are all NaN
Try this:
self.__stoch = indicator.STOCH(feed[instrument],fastk_period,slowk_period,slowd_period)
The stochastic oscillator is expecting a bar dataseries, not a regular one.

Categories

Resources