Monday, 1 February 2021

Numpy Where Multiple Conditions , cannot compare a dtyped [object] array with a scalar of type [bool]

I am trying to do multiple conditions in a numpy where but getting the error: cannot compare a dtyped [object] array with a scalar of type [bool]

Here is the line:

d7['CAD'] = np.where(d7['Category'] == 'Stack' & d7['Currency'] == fxratetable['from_currency'],d7['CAD'] * fxratetable['fx_rate'], d7['CAD'])

All dtypes are object except for fx_rate which is float64.

Also another thought is that I am looking for one value with Category = Stack but looking for multiple values with my Currency = from_currency.

Can anyone help with this?

Thanks.

new error

ValueError: Can only compare identically-labeled Series objects now. 

This is my new statement

d7['CAD'] = np.where((d7['Category'] == 'Stack') & 
                     (d7['Currency'] == fxratetable['from_currency']),
                     d7['CAD'] * fxratetable['fx_rate'], 
                     d7['CAD'])

d7:

+--------------+----------+----------+
| CAD          | Currency | Category |
+--------------+----------+----------+
| -4350242.355 | GBP      | Stack    |
+--------------+----------+----------+
| 424223.7584  | AUD      | Stack    |
+--------------+----------+----------+

fxratetable:

+---------------+---------+
| from_currency | fx_rate |
+---------------+---------+
| GBP           | 1.367   |
+---------------+---------+
| AUD           | 0.7706  |
+---------------+---------+

Expected new CAD column.

+----------------+
| CAD (expected) |
+----------------+
| -5948957.275   |
+----------------+
| 326991.5663    |
+----------------+


from Numpy Where Multiple Conditions , cannot compare a dtyped [object] array with a scalar of type [bool]

No comments:

Post a Comment