jmbejara / comp-econ-sp19

Main Course Repository for Computational Methods in Economics (Econ 21410, Spring 2019)
48 stars 26 forks source link

Midterm solutions type error #23

Closed rhuselid closed 5 years ago

rhuselid commented 5 years ago

I was working through last year's midterm and I ran into bug where I got a type error whenever I tried to use pivot tables. When I ran the given solutions, I got the same error:

TypeError: Cannot cast array data from dtype('float64') to dtype('<U32') according to the rule 'safe'

This was from question 6 and 8 from last year's midterm. I am running this on jupyter notebook

rhuselid commented 5 years ago

Update: when I ran the solution in colab I had the same error.

jmbejara commented 5 years ago

Hi. Thanks for finding this. It seems that there is an issue with pivot_table using Categorical variables. This wasn't a problem last year. You can fix this by changing the "loading" code to the following:

euro12 = pd.read_csv('https://raw.githubusercontent.com/jokecamp/FootballData/master/UEFA_European_Championship/Euro%202012/Euro%202012%20stats%20TEAM.csv', sep=',')
euro12['Shooting Accuracy'] = euro12['Shooting Accuracy'].str[:-1].astype(float)
quartiles = [0, .25, .5, .75, 1.]
labels = ['Q1', 'Q2', 'Q3', 'Q4']
# labels = None
euro12['passes_quartile'] = pd.qcut(euro12['Passes'], q=quartiles, labels=labels)
euro12['shoot_acc_quartile'] = pd.qcut(euro12['Shooting Accuracy'], q=quartiles, labels=labels)
euro12.dtypes

That is, use the quartile labels that were given. Thanks!