1
votes

I'm trying to convert a .csv file into a dataframe. I have already done this so many times I cannot count them. However, when I write:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
os.chdir("/Users/federico/Documents/Polimi/thesis/modellini/failures_and_failures_again")
pd.read_csv(r'irr.csv',sep=",",index_col=0)

I always get this:

File "", line 1, in runfile('/Users/federico/.spyder-py3/temp.py', wdir='/Users/federico/.spyder-py3')

File "/Users/federico/opt/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 827, in runfile execfile(filename, namespace)

File "/Users/federico/opt/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "/Users/federico/.spyder-py3/temp.py", line 9, in pd.read_csv(r'irr.csv',sep=",",index_col=0)

File "/Users/federico/opt/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py", line 685, in parser_f return _read(filepath_or_buffer, kwds)

File "/Users/federico/opt/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py", line 457, in _read parser = TextFileReader(fp_or_buf, **kwds)

File "/Users/federico/opt/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py", line 895, in init self._make_engine(self.engine)

File "/Users/federico/opt/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py", line 1135, in _make_engine self._engine = CParserWrapper(self.f, **self.options)

File "/Users/federico/opt/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py", line 1917, in init self._reader = parsers.TextReader(src, **kwds)

File "pandas/_libs/parsers.pyx", line 382, in pandas._libs.parsers.TextReader.cinit

File "pandas/_libs/parsers.pyx", line 693, in pandas._libs.parsers.TextReader._setup_parser_source

OSError: Initializing from file failed

The .csv file has been downloaded from PvGis https://re.jrc.ec.europa.eu/pvg_tools/en/tools.html#PVP and besides the first eight rows, it contains only data. There seems to be nothing strange in it, nor anything encoded in any particular way.

I really cannot understand where does it come from.

EDIT:

I eventually managed to convert it into a data frame on Google Colab, and create through pd.to_csv the same identical file. Spyder still doesn't want to open event the downloaded file.

1
It's unlikely to be the issue, but can you try it without the chdir? It's probably the better practice anyway. The CSV file is valid, nothing unusual?AMC
I tried without, still the same problem. The CSV file seems to be ok. Any hint for a good validation, though? csvlint.io does not give me back any issueFederico Guermandi

1 Answers

0
votes

Validate your csv via https://csvlint.io/ or test with some new made-up .csv file. Wrong file path could also be an error so id try for the peace of mind to drag csv into the workspace directory and pass the name of the file as a parameter.