Adhish Aggarwal
Adhish Aggarwal

Reputation: 1

pandas read_csv on dynamic URL gives EmptyDataError: No columns to parse from file

I am learning Python from 'Python for Finance' and the packages used are outdated. Please help me out with the following error: It shows EmptyDataError: No columns to parse from file.

import pandas as pd
url1 = 'http://hopey.netfonds.no/posdump.php?'
url2 = 'date=%s%s%s&paper=AAPL.O&csv_format=csv'
url = url1 + url2

year = '2017'
month = '11'
days = ['9']

AAPL = pd.DataFrame()
for day in days:
    AAPL = AAPL.append(pd.read_csv(url % (year, month, day),
                                   index_col=0, header=0, parse_dates=True))

AAPL.columns = ['bid', 'bdepth', 'bdeptht','offer', 'odepth', 'odeptht']

Error:

EmptyDataError: No columns to parse from file

Upvotes: 0

Views: 1857

Answers (1)

Bui Dinh Ngoc
Bui Dinh Ngoc

Reputation: 447

This error is caused by a Pandas issue.

  1. Pandas will send a GET request to get file content. But this url is a dynamic script that does not print content directly.
  2. posdump.php script will send HTTP response like:
    HTTP/1.1 200 OK
    Date: Tue, 22 May 2018 01:18:42 GMT
    Server: Apache/2.4.7 (Ubuntu)
    X-Powered-By: PHP/5.5.9-1ubuntu4.25
    Content-Disposition: attachment; filename=exams.csv
    Pragma: no-cache
    Expires: 0
    Content-Type: text/csv

It means content is sent in an attachment named filename exams.csv.

  1. To fix this, report this issue to pandas team.

  2. For a workaround, please use requests module to get the content as string, then read in with pandas:

    from io import StringIO

    import pandas as pd
    import requests
    url = 'https://raw.githubusercontent.com/cs109/2014_data/master/countries.csv'
    s = requests.get(url).text    
    c = pd.read_csv(StringIO(s))

Upvotes: 1

Related Questions