0

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>
3
  • Is Series pandas.Series? Commented Nov 23, 2013 at 23:56
  • Yeah I read the csv with pandas, the column header of the series in the dataframe is d.typ_str Commented Nov 24, 2013 at 0:03
  • I added the first few lines before Commented Nov 24, 2013 at 0:07

1 Answer 1

2

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'
Sign up to request clarification or add additional context in comments.

1 Comment

+1, not for a cool answer but for being able to guess the original intent from the question and error. BTW if this is int to string, how about usd_trades['d.type_str'] = np.choose(usd_trades['d.type_str'], ['bid','ask'])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.