user2113095
user2113095

Reputation: 1067

Python string compare error

I am getting the following error when converting my binary d.type_str variable to 'bid' or 'ask'. Thanks for the help guys! I'm using python 2.7

My code:

from itertools import izip_longest
import itertools
import pandas 
import numpy as np

all_trades = pandas.read_csv('C:\\Users\\XXXXX\\april_trades.csv', parse_dates=[0], index_col=0)
usd_trades = all_trades[all_trades['d.currency'] == 'USD']

volume = (usd_trades['d.amount_int'])
trades = (usd_trades['d.price_int'])

def cleanup(x):
    if isinstance(x, str) and 'e-' in x:
        return 0
    else:
        return float(x)

volume = volume.apply(lambda x: cleanup(x))
volume = volume.astype(float32)

##### 
typestr = (usd_trades['d.type_str'])
typestr[typestr == 'bid'] = 0 
typestr[typestr == 'ask'] = 1

Error output:

>>> typestr[typestr == 'ask'] = 1
  File "C:\Anaconda\lib\site-packages\pandas\core\series.py", line 240, in wrapper
    % type(other))
TypeError: Could not compare <type 'str'> type with Series
>>> Traceback (most recent call last):
  File "<stdin>", line 1, in <module>

Upvotes: 0

Views: 1046

Answers (1)

alko
alko

Reputation: 48317

As you stated, your typestr is binary. Pandas complains when you try to compare string with serise with int data, see

>>> s = pd.Series([1], dtype=np.int64)
>>> s == 'a'
Traceback (most recent call last):
   ...
TypeError: Could not compare <type 'str'> type with Series

From your text I guess you want instead do

>>> typestr[typestr == 1] = 'ask'

Upvotes: 2

Related Questions