0

I'm parsing particular columns from a number of cvs files into one dataframe. However I get an error :

ValueError: No columns to parse from file

I think the error stems from trying to read a csv with no data (just an empty file) and would like to know how I can get around the problem? My code is as follows:

def getTimeseriesData(DataPath,columnNum):
    colNames = ['date']

    path = DataPath
    filePath = path, "*.csv"
    allfiles = glob.glob(os.path.join(path, "*.csv"))
    for fname in allfiles:
        name = os.path.splitext(fname)[0]
        name = os.path.split(name)[1]

        colNames.append(name)
    print(colNames)

    dataframes = [pd.read_csv(fname, header=None,usecols=[0,columnNum]) for fname in allfiles]
    print(dataframes)    

    reduce(partial(pd.merge, on=0, how='outer'), dataframes)

    timeseriesData = reduce(partial(pd.merge, on=0, how='outer'), dataframes)


    timeseriesData.columns=colNames

    return timeseriesData

my full stack trace is as follows:

  File "C:\Users\Documents\scripts\AuctionStrategy_2.1.py", line 340, in <module>
    main()

  File "C:\Users\Documents\scripts\AuctionStrategy_2.1.py", line 39, in main
    stockData = getTimeseriesData2(rawTimeseriesDataPath,1)

  File "C:\Users\Documents\scripts\AuctionStrategy_2.1.py", line 88, in getTimeseriesData2
    dataframes = [pd.read_csv(fname, header=None,usecols=[0,columnNum]) for fname in allfiles]

  File "C:\Users\Documents\scripts\AuctionStrategy_2.1.py", line 88, in <listcomp>
    dataframes = [pd.read_csv(fname, header=None,usecols=[0,columnNum]) for fname in allfiles]

  File "C:\WinPython-64bit-3.4.4.1\python-3.4.4.amd64\lib\site-packages\pandas\io\parsers.py", line 498, in parser_f
    return _read(filepath_or_buffer, kwds)

  File "C:\WinPython-64bit-3.4.4.1\python-3.4.4.amd64\lib\site-packages\pandas\io\parsers.py", line 275, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)

  File "C:\WinPython-64bit-3.4.4.1\python-3.4.4.amd64\lib\site-packages\pandas\io\parsers.py", line 590, in __init__
    self._make_engine(self.engine)

  File "C:\WinPython-64bit-3.4.4.1\python-3.4.4.amd64\lib\site-packages\pandas\io\parsers.py", line 731, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)

  File "C:\WinPython-64bit-3.4.4.1\python-3.4.4.amd64\lib\site-packages\pandas\io\parsers.py", line 1103, in __init__
    self._reader = _parser.TextReader(src, **kwds)

  File "pandas\parser.pyx", line 518, in pandas.parser.TextReader.__cinit__ (pandas\parser.c:5030)

ValueError: No columns to parse from file
AMC
  • 2,642
  • 7
  • 13
  • 35
Stacey
  • 4,825
  • 17
  • 58
  • 99

1 Answers1

1

It has been a few years since I last used python but I would catch the exception and handle it from there.

From the python documentation

Handling exceptions

EDIT:

from stackoverflow: Try/Except in Python: How do you properly ignore Exceptions?

Is there a CSV-file of a 0 Byte size in your folder?

best regards

Community
  • 1
  • 1
motaa
  • 327
  • 2
  • 11