Learner Algorithm Learner Algorithm - 13 days ago 14
Python Question

IOError for python 2.7 and mac

I have a script as follows which originally is written for Windows and I am trying to get it run on Mac:
I get error that a file does not exist while I have the file there in the folder, would you please tell me where I am making a mistake ?

import pandas as pd
def merge(peak_score, profile_score, res_file):
df_peak = pd.DataFrame.from_csv(peak_score, index_col = False, sep='\t')
df_profile = pd.DataFrame.from_csv(profile_score, index_col = False, sep='\t')
result = pd.concat([df_peak, df_profile], axis=1)
print result.head()
test = []
for a,b in zip(result['prot_a_p'],result['prot_b_p']):
if a == b:
test.append(1)
else:
test.append(0)
result['test']=test
result = result[result['test']==0]
del result['test']
result = result.fillna(0)
result.to_csv(res_file)

if __name__ == '__main__':
pass


I am getting an error shown below.

Traceback (most recent call last):
File "main.py", line 194, in <module>
start_path+end_res)
File "/Users/admin/Desktop/script/mergeT.py", line 5, in merge
df_peak = pd.DataFrame.from_csv(peak_score, index_col = False, sep='\t')
File "/Library/Python/2.7/site-packages/pandas/core/frame.py", line 1231, in from_csv
infer_datetime_format=infer_datetime_format)
File "/Library/Python/2.7/site-packages/pandas/io/parsers.py", line 645, in parser_f
return _read(filepath_or_buffer, kwds)
File "/Library/Python/2.7/site-packages/pandas/io/parsers.py", line 388, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "/Library/Python/2.7/site-packages/pandas/io/parsers.py", line 729, in __init__
self._make_engine(self.engine)
File "/Library/Python/2.7/site-packages/pandas/io/parsers.py", line 922, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "/Library/Python/2.7/site-packages/pandas/io/parsers.py", line 1389, in __init__
self._reader = _parser.TextReader(src, **kwds)
File "pandas/parser.pyx", line 373, in pandas.parser.TextReader.__cinit__ (pandas/parser.c:4175)
File "pandas/parser.pyx", line 667, in pandas.parse**strong text**r.TextReader._setup_parser_source (pandas/parser.c:8440)
IOError: File results\scoring\fed\score_peak.txt does not exist


I have tried to set a path to the exact file
for example

peak_score = '/Users/admin/Desktop/peak_score.txt'
profile_score = '/Users/admin/Desktop/profile_score.txt'


now the error changed to

Traceback (most recent call last):
File "main.py", line 194, in <module>
start_path+end_res)
File "/Users/admin/Desktop/script/mergeT.py", line 5, in merge
df_peak = pd.DataFrame.from_csv(peak_score, index_col = False, sep='\t')
File "/Library/Python/2.7/site-packages/pandas/core/frame.py", line 1231, in from_csv
infer_datetime_format=infer_datetime_format)
File "/Library/Python/2.7/site-packages/pandas/io/parsers.py", line 645, in parser_f
return _read(filepath_or_buffer, kwds)
File "/Library/Python/2.7/site-packages/pandas/io/parsers.py", line 388, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "/Library/Python/2.7/site-packages/pandas/io/parsers.py", line 729, in __init__
self._make_engine(self.engine)
File "/Library/Python/2.7/site-packages/pandas/io/parsers.py", line 922, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "/Library/Python/2.7/site-packages/pandas/io/parsers.py", line 1435, in __init__
self._set_noconvert_columns()
File "/Library/Python/2.7/site-packages/pandas/io/parsers.py", line 1500, in _set_noconvert_columns
_set(self.index_col)
File "/Library/Python/2.7/site-packages/pandas/io/parsers.py", line 1475, in _set
x = names.index(x)
ValueError: False is not in list

Answer

As per documentation of pandas 0.19.1 pandas.DataFrame.from_csv does not support index_col = False. Try to use pandas.read_csv instead (with the same parameters). Also make sure you are using the up to date version of pandas.

See if this works:

import pandas as pd
def merge(peak_score, profile_score, res_file):
    df_peak = pd.read_csv(peak_score, index_col = False, sep='\t')
    df_profile = pd.read_csv(profile_score, index_col = False, sep='\t')
    result = pd.concat([df_peak, df_profile], axis=1)
    print result.head()
    test = []
    for a,b in zip(result['prot_a_p'],result['prot_b_p']):
        if a == b:
            test.append(1)
        else:
            test.append(0)
    result['test']=test
    result = result[result['test']==0] 
    del result['test']
    result = result.fillna(0)
    result.to_csv(res_file)   

if __name__ == '__main__':
    pass

Regarding the path issue when changing from Windows to OS X:

In all flavours of Unix, paths are written with slashes /, while in Windows backslashes \ are used. Since OS X is a descendant of Unix, as other users have correctly pointed out, when you change there from Windows you need to adapt your paths.

Comments