1

I have use the following code to extract column from AZ to CW:

import numpy as np
import pandas as pd

url = 'https://www.dropbox.com/s/yze3laidhv7gl1x/Homework4.xlsx?dl=1'

changedate = lambda x: pd.to_datetime(x, format = "%Y%m") 

df = pd.read_excel(url, sheet_name = '49_Industry_Portfolios', header = 6, 
                   parse_cols = 'AZ:CW', index_col = 0, parse_dates = True, 
                   date_parser = changedate, na_values = [-99.99, -999])
print(df)

But it raises a Traceback, anyone could help me to fix this issue? Thanks.

Out:

Traceback (most recent call last):

  File "<ipython-input-5-671759d2c7d1>", line 17, in <module>
    date_parser = changedate, na_values = [-99.99, -999])

  File "/Users/x/anaconda3/lib/python3.6/site-packages/pandas/util/_decorators.py", line 296, in wrapper
    return func(*args, **kwargs)

TypeError: read_excel() got an unexpected keyword argument 'parse_cols'

2 Answers 2

3

The problem is caused because parse_cols is deprecated, use usecols instead.

df = pd.read_excel(url, sheet_name = '49_Industry_Portfolios', header = 6, 
                   usecols = 'AZ:CW', index_col = 0, parse_dates = True, 
                   date_parser = changedate, na_values = [-99.99, -999])
Sign up to request clarification or add additional context in comments.

Comments

1

A way to use usecols for a given number (let say nb_cols) of first columns is:

pd.read_excel(..., usecols=range(nb_cols), ...).

For PEP8 compliance, do not use space for keyword (named) arguments in function call

For start and end columns:

pd.read_excel(..., usecols=range(start, end), ...).

Comments

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.