Фрейм данных, содержащий значения, не работает с "np.where" и существует ошибка valueer - PullRequest
0 голосов
/ 06 марта 2020

Я написал следующий код в python, чтобы скорректировать мои валюты, основываясь на долларах США. Отлично работает по частям, но в целом выдает следующую ошибку: ValueError: операнды не могут передаваться вместе с фигурами (0,8) (17072,) (17072,)

Может кто-нибудь пожалуйста помогите мне найти проблему?

Ура

other_currencies = ['AUD','EUR','GBP','NZD']
other_currencies_plus = ['AUD','EUR','GBP','NZD','USD']
spot_spread_pivotted['Ccy'] = pd.np.where(spot_spread_pivotted[(spot_spread_pivotted['BaseCCY'].isin(other_currencies)) & (spot_spread_pivotted['PriceCCY'] == 'USD') ],
                               spot_spread_pivotted['BaseCCY'] + ('_' + spot_spread_pivotted['PriceCCY']),
                   pd.np.where(spot_spread_pivotted[(spot_spread_pivotted['PriceCCY'].isin(other_currencies)) & (spot_spread_pivotted['BaseCCY'] == 'USD') ], 
                               spot_spread_pivotted['PriceCCY'] + ('_' + spot_spread_pivotted['BaseCCY']),
                   pd.np.where(spot_spread_pivotted[(spot_spread_pivotted['BaseCCY'].isin(other_currencies)) & (spot_spread_pivotted['PriceCCY'].isin(other_currencies)) ],
                              ( spot_spread_pivotted['BaseCCY'] + '_' + 'USD') + ('/' + spot_spread_pivotted['PriceCCY'] + '_' + 'USD'),
                   pd.np.where(spot_spread_pivotted[(spot_spread_pivotted['BaseCCY'].isin(other_currencies)) & (~spot_spread_pivotted['PriceCCY'].isin(other_currencies_plus)) ],
                               (spot_spread_pivotted['BaseCCY'] + '_' + 'USD') + ('/' + 'USD' + '_' + spot_spread_pivotted['PriceCCY']),
                   pd.np.where(spot_spread_pivotted[(spot_spread_pivotted['PriceCCY'].isin(other_currencies)) & (~spot_spread_pivotted['BaseCCY'].isin(other_currencies_plus)) ],
                              ('USD' + '_' + spot_spread_pivotted['BaseCCY']) + ('/' +  spot_spread_pivotted['PriceCCY'] + '_' + 'USD')  ,
                   (('USD' + '_' + spot_spread_pivotted['BaseCCY']) + ('/' + 'USD' + '_' + spot_spread_pivotted['PriceCCY'])) )))))

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~







1 Ответ

0 голосов
/ 13 марта 2020

Я понял, что для критических условий использование np.where не совсем правильно, так как вы пропустите скобки в синтаксисе и использование if не совсем правильно, поскольку это делает код очень медленным. лучшее решение будет иметь ваше состояние и результат отдельно, как показано ниже:

m = [(spot_table['Buy_Currency'].isin(other_currencies)) & (spot_table['Sell_Currency'] == 'USD'), 
     (spot_table['Sell_Currency'].isin(other_currencies)) & (spot_table['Buy_Currency'] == 'USD'), 
     (spot_table['Buy_Currency'].isin(other_currencies)) & (spot_table['Sell_Currency'].isin(other_currencies)),
     (spot_table['Buy_Currency'].isin(other_currencies)) & (~spot_table['Sell_Currency'].isin(other_currencies_plus)),
     (spot_table['Sell_Currency'].isin(other_currencies)) & (~spot_table['Buy_Currency'].isin (other_currencies_plus))]          

, а затем решение, как показано ниже:

spot_table['Ccy'] = np.select(m, [spot_table['Buy_Currency'] + ('_' + spot_table['Sell_Currency']),
                                  spot_table['Sell_Currency'] + ('_' + spot_table['Buy_Currency']),
                                  ( spot_table['Buy_Currency'] + '_' + 'USD') + ('/' + spot_table['Sell_Currency'] + '_' + 'USD'), 
                                  (spot_table['Buy_Currency'] + '_' + 'USD') + ('/' + 'USD' + '_' + spot_table['Sell_Currency']),
                                  ('USD' + '_' + spot_table['Buy_Currency']) + ('/' +  spot_table['Sell_Currency'] + '_' + 'USD')],
          ('USD' + '_' + spot_table['Buy_Currency']) + ('/' + 'USD' + '_' + spot_table['Sell_Currency']))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...