Executing my Python code containing pandas df slicing with .ix, I get a warning about its future deprecation. I have been trying to find a replacement. But can for the life of me not get the code to work.
I have followed this answer pandas iloc vs ix vs loc explanation, how are they different? and many more
If you try to remove the 2 lines with .loc slicing, and replace it with the line above that uses .ix, you may see the code work properly.
Please help.
My code that fetches updated excange-rates from the US Federal Reserve Bank.
module = __name__
import pandas as pd
pd.core.common.is_list_like = pd.api.types.is_list_like
from pandas_datareader import data as web
import numpy as np
import datetime
xratelist = ['DEXDNUS', 'DEXUSEU']
xrts = []
def xRateList_pd(xratelist, modus='sim'):
years = 1.2
days = int(252 * years) # ant. arb. dage pr år = 252
if modus == 'sim':
start = datetime.datetime(2016,1,1) # indstil manuelt
end = datetime.datetime(2018,5,18) # indstil manuelt
if modus == 'trading':
end = pd.Timestamp.utcnow()
start = end - days * pd.tseries.offsets.BDay()
# Pick all rates simultaneously
print('Fetching xratelist from Fred: ', xratelist)
for xrt in xratelist:
r = web.DataReader(xrt, 'fred',
start = start, end = end)
# add a symbol column
xrts.append(r)
# concatenate all the dfs into one
df_xrates = pd.concat(xrts, axis='columns')
df_xrates['DEXDNUS'] = np.round(df_xrates['DEXDNUS'], decimals=4)
df_xrates['DEXUSEU'] = np.round(df_xrates['DEXUSEU'], decimals=4)
# xrateDNUS = float(df_xrates.ix[-1, 'DEXDNUS']) # Works but deprecated...
xrateDNUS = float(df_xrates.loc[-1, 'DEXDNUS']) # Finds last x-rate in pd
xratedateDNUS = df_xrates.index[-1] # Finds date for this xrate, index..
print('Found xrateDNUS DKK/USD : %1.2f DKK, from: %s' %(xrateDNUS, xratedateDNUS))
# xrateUSEU = float(df_xrates.ix[-1, 'DEXUSEU']) # Works but deprecated...
xrateUSEU = float(df_xrates.loc[-1, 'DEXUSEU'])
xratedateUSEU = df_xrates.index[-1]
print('Found xrateUSEU USD/EUR : %1.2f $, from: %s' %(xrateUSEU, xratedateUSEU))
return df_xrates, xrateDNUS, xratedateDNUS, xrateUSEU, xratedateUSEU
try:
### Get Xrates =============================================================
print('')
print(' ==== $ ====')
print('')
print('Looking up DKK/USD & USD/EUR online (US Federal Reserve Bank)') # via Federal Res. Bank (using pd DataReader)'
df_xrates, xrateDNUS, xratedateDNUS, xrateUSEU, xratedateUSEU = xRateList_pd(xratelist, modus='trading') #,years=0.25 ,start=datetime.datetime(2000,1,1), end=pd.Timestamp.utcnow()
except:
print('!!! Error in module %s !!!' %(module))
pass
if __name__ == '__main__':
print("== m03_get_x_rates == is being run directly")
else:
print("Importing and processing == %s == from CrystallBall" %(module))