2

I'm basically running some code as follows. Basically I'm just retrieving pairs of stocks (laid out as Row 1-Stock 1,2, Row 2-Stock 1,2 and so on, where Stock 1 and 2 are different in each row) from a CSV File. I then take in data from Yahoo associated with these "Pairs" of Stocks. I calculate the returns of the stocks and basically check if the distance (difference in returns) between a pair of stocks breaches some threshold and if so I return 1. However, I'm getting the following error which I am unable to resolve:

PricePort(tickers)
     27     for ticker in tickers:
     28         #print ticker
---> 29         x = pd.read_csv('http://chart.yahoo.com/table.csv?s=ttt'.replace('ttt',ticker),usecols=[0,6],index_col=0)
     30         x.columns=[ticker]
     31         final=pd.merge(final,x,left_index=True,right_index=True)

TypeError: expected a character buffer object 

The code is as follows:

from datetime import datetime
import pytz
import csv
import pandas as pd
import pandas.io.data as web
import numpy as np

#Retrieves pairs of stocks (laid out as Row 1-Stock 1,2, Row 2-Stock 1,2 and   so on, where Stock 1 and 2 are different in each row) from CSV File
def Dataretriever():
        Pairs = []
        f1=open('C:\Users\Pythoncode\Pairs.csv') #Enter the location of the file
        csvdata= csv.reader(f1)
        for row in csvdata:          #reading tickers from the csv file
            Pairs.append(row)
        return Pairs

tickers = Dataretriever() #Obtaining the data

#Taking in data from Yahoo associated with these "Pairs" of Stocks
def PricePort(tickers):
    """
        Returns historical adjusted prices of a portfolio of stocks.
        tickers=pairs
    """
    final=pd.read_csv('http://chart.yahoo.com/table.csv?s=^GSPC',usecols=[0,6],index_col=0)
    final.columns=['^GSPC']
    for ticker in tickers:
        #print ticker
        x = pd.read_csv('http://chart.yahoo.com/table.csv?s=ttt'.replace('ttt',ticker),usecols=[0,6],index_col=0)
        x.columns=[ticker]
        final=pd.merge(final,x,left_index=True,right_index=True)
    return final   

#Calculating returns of the stocks
def Returns(tickers):
    l = []
    begdate=(2014,1,1)
    enddate=(2014,6,1)    
    p = PricePort(tickers)
    ret = (p.close[1:] - p.close[:-1])/p.close[1:]
    l.append(ret)
    return l

#Basically a class to see if the distance (difference in returns) between a 
#pair of stocks breaches some threshold
class ThresholdClass():    
    #constructor
    def __init__(self, Pairs):
        self.Pairs = Pairs

    #Calculating the distance (difference in returns) between a pair of stocks
    def Distancefunc(self, tickers):
        k = 0
        l = Returns(tickers)
        summation=[[0 for x in range (k)]for x in range (k)]      #2d matrix for the squared distance
        for i in range (k):
            for j in range (i+1,k):     # it will be a upper triangular matrix
                for p in range (len(self.PricePort(tickers))-1):
                    summation[i][j]= summation[i][j] + (l[i][p] - l[j][p])**2       #calculating distance

        for i in range (k):     #setting the lower half of the matrix 1 (if we see 1 in the answer we will set a higher limit but typically the distance squared is less than 1)
            for j in range (i+1):
                sum[i][j]=1
            return sum

    #This function is used in determining the threshold distance
    def MeanofPairs(self, tickers):
        sum = self.Distancefunc(tickers)
        mean = np.mean(sum)
        return mean

    #This function is used in determining the threshold distance
    def StandardDeviation(self, tickers):
        sum = self.Distancefunc(tickers)
        standard_dev = np.std(sum)
        return standard_dev 


    def ThresholdandnewsChecker(self, tickers):
        threshold = self.MeanofPairs(tickers) + 2*self.StandardDeviation(tickers)
        if (self.Distancefunc(tickers) > threshold):
            return 1 

Threshold_Class  = ThresholdClass(tickers)   
Threshold_Class.ThresholdandnewsChecker(tickers,1)
0

1 Answer 1

1

The trouble is Dataretriever() returns a list, not a string. When you iterate over tickers(), the name ticker is bound to a list.

The str.replace method expects both arguments to be strings. The following code raises the error because the second argument is a list:

'http://chart.yahoo.com/table.csv?s=ttt'.replace('ttt', ticker)

The subsequent line x.columns = [ticker] will cause similar problems. Here, ticker needs to be a hashable object (like a string or integer), but lists are not hashable.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks. I edited my code as follows, yet I still have the same issue: tickersasstrings = map(str, tickers) def PricePort(tickersasstrings): final=pd.read_csv('http://chart.yahoo.com/table.csv?s=^GSPC',usecols=[0,6],index_col=0) final.columns=['^GSPC'] for ticker in tickersasstrings: #print ticker x = pd.read_csv('http://chart.yahoo.com/table.csv?s=ttt'.replace('ttt',ticker),usecols=[0,6],index_col=0) x.columns=[ticker] final=pd.merge(final,x,left_index=True,right_index=True) return final
I'd guess it's because of the x.columns=[ticker] bit. x.columns needs to assigned to a list strings or numbers (hashable) not a list of lists (unhashable).
Thanks Again. I tried changing it to x.columns = ticker as it would then just be assigned a list of strings, however this does not appear to resolve the issue. Could there be another problem?
Nothing jumps out - what's the new error message you're getting?
Its the same one as before.
|

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.