执行包含用.ix切片的df切片的Python代码,我收到有关其未来弃用的警告。我一直在寻找替代品。但是我一生都无法使代码正常工作。
我已经遵循了这个答案 pandas iloc vs ix vs loc explanation, how are they different? 还有更多
如果尝试使用.loc切片删除两行,并用上面使用.ix的行替换,您可能会看到代码正常工作。
请帮助。
我的代码可从美国联邦储备银行获取最新汇率。
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))
答案 0 :(得分:0)
谢谢大家,我很清楚我在问题中所写的.iloc的可能性。但是在我的示例中,我无法破解该方法。
好-终于2天后,我弄破了螺母。我找到了一种用.iat切片df的新方法,并且可以工作! ew。
因此,不建议使用.ix的切片行
xrateDNUS = float(df_xrates.ix[-1, 'DEXDNUS']) # Works but deprecated...
被这种更现代的.iat切片方式所取代
xrateDNUS = float(df_xrates.iat[-1, 0]) # Finds last x-rate in pd
,并且有效。
实际上,我也使用相同的切片引用语法使.iloc起作用
xrateDNUS = float(df_xrates.loc[-1, 0]) # Finds last x-rate in pd
奇怪的是我无法让.loc与列标签一起使用...我想知道...
xrateDNUS = float(df_xrates.iloc[-1, 'DEXDNUS'])
如果任何人都可以理解为什么.iloc不起作用,请发表评论。...