With this script/module, XRateDKKUSD_test.py, I can successfully fetch the exchange rate DKK pr USD.
import pandas as pd
import pandas.io.data as web
import datetime
def xRate_pd(years,modus,start=datetime.datetime(2000,1,1),end=pd.Timestamp.utcnow()):
global xrate, xratedate, df_xrate
days = int(252 * years) # ant. arb. dage pr år = 252
if modus == 'sim':
start = datetime.datetime(2014,1,1) # indstil manuelt
end = datetime.datetime(2015,5,18) # indstil manuelt
if modus == 'trading':
end = pd.Timestamp.utcnow()
start = end - days * pd.tseries.offsets.BDay()
df_xrate = web.DataReader('DEXDNUS', 'fred',
start = start, end = end)
print('df_xrate \n',df_xrate)
# Selecting only last day from df, saving to xrate, xratedate
xrate = df_xrate.ix[-1, 'DEXDNUS']
xratedate = df_xrate.index[-1]
return xrate, xratedate, df_xrate
if __name__ == '__main__':
# xrate_lookup()
xRate_pd(modus='trading',years=0.25)
However, when I try to run this script from my main program with this function...
def xRate(start, end, years, modus):
global xrate, xratedate, df_xrate
xrate, xratedate, df_xrate = XRateDKKUSD_test.xRate_pd(start, end, modus)
return xrate, xratedate, df_xrate
Run with this call
import XRateDKKUSD_test
xRate_pd(start, end)
Obviously I have set the 'start' & 'end' parameters in a preceeding function.
when the script is run imported as a module I suddenly run into this problem that I do not get when the script is run stand-alone:
File "z:/python/crystallball/git - crystalball/_crystalball_main.py", line 277, in <module>
xRate_pd(start, end)
File "Z:/python/CrystallBall/Git - CrystalBall/XRateDKKUSD.py", line 55, in xRate_pd
days = int(252 * years)
TypeError: unsupported operand type(s) for *: 'int' and 'Timestamp'
Anyone who knows why there is this difference, and error when I import and run the script?
Your script has no issues with being imported as a module. You are passing in a different type of object for the years
argument.
When you call the code from the if __name__ == '__main__':
guard, you pass in years
as a float:
xRate_pd(modus='trading', years=0.25)
which works fine for your multiplication:
days = int(252 * years) # ant. arb. dage pr år = 252
but when you call your function after importing, years
is set from the variable start
:
xRate_pd(start, end)
and start
is not a float
but a Timestamp
instance, as evidenced by the error message:
TypeError: unsupported operand type(s) for *: 'int' and 'Timestamp'
The int
here is the 252
literal.