Python course notes - errata

Author: Bill Tubbs

Is this the right place to post possible errors/typos in the lecture notes?

In the Solow model code example on this page:
http://quant-econ.net/py/python_oop.html

The class’s docstring doesn’t seem to come out right.

In [5]: print Solow.__doc__

    Implements the Solow growth model with update rule

    .. math::
        k_{t+1} = 
                  rac{s z k^{lpha}_t}{1 + n}  + k_t 
                                                    rac{1 + d}{1 + n}

Hi Bill,

Yes, this is a good place to post errors and typos and this is much appreciated.

You are right. The docstring was missing an ‘r’ in front of the string, to indicate that it should be read as a “raw string”. Otherwise those backslashes are interpreted as special characters.

I’ve fixed it. The change is visible in line 9 of

https://github.com/QuantEcon/QuantEcon.applications/blob/master/python_oop/solow.py

Thanks again,

John.

Author: Bill Tubbs

Thanks John.

Another thing I noticed going through the web pages is some strange notation in class definitions. In particular the def statement.

For example this piece of code on the same website I reference above:

class Foo:

    d.. __len__(self)_py:
        return 42

Bill.

Thanks Bill,

One of our scripts that compiles the site seems to be tripping up on that bit of code. I’ll flag it and we’ll get it fixed soon.

Please do continue to post if you find errors. It’s a big help.

Regards, John.

Author: Bill Tubbs

From the page:
http://quant-econ.net/py/scipy.html

The following line of code:

identifier = scipy.stats.distribution_name(shape_parameters, `loc=c`, `scale=d`)

Should it be:

identifier = scipy.stats.distribution_name(shape_parameters, loc=c, scale=d)

Author: Bill Tubbs

On page:
http://quant-econ.net/py/scipy.html

Typo:
#There are other options for univeriate integration

Should be:
##There are other options for univariate integration

Author: Bill Tubbs

On page:
http://quant-econ.net/py/pandas.html

I got a warning when executing the following line of code:

df['population'] = df['population'] * 1e3

Warning:

/Users/billtubbs/anaconda/lib/python2.7/site-packages/ipykernel/__main__.py:3: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
app.launch_new_instance()

I think the warning is something to do with the fact that the object on the left is a copy of the DataFrame.

I think the correct code is:

df.loc[:, 'population'] = df['population'] * 1e3

See here:

Author: Bill Tubbs

On page:
http://quant-econ.net/py/pandas.html

df = df.sort_index(by='GDP percap', ascending=False)

I got a warning saying this is deprecated.

/Users/billtubbs/anaconda/lib/python2.7/site-packages/ipykernel/main.py:2: FutureWarning: by argument to sort_index is deprecated, pls use .sort_values(by=…)
from ipykernel import kernelapp as app

Should be:

df = df.sort_values(by='GDP percap', ascending=False)

Thanks for pointing these out Bill - these have now been fixed in the latest source.

Thanks Bill - this is now updated to use sort_values() method

Hi Bill,

Could you please post which version of pandas that you are using. Thanks.

Best Regards,
Matt

Fixed - Thanks Bill.

Fixed - Thanks Bill.

Author: Bill Tubbs

On page:
http://quant-econ.net/py/need_for_speed.html

When I enter the following into an iPython notebook:

%load_ext cythonmagic

I get the warning:

/Users/billtubbs/anaconda/lib/python2.7/site-packages/IPython/extensions/cythonmagic.py:21: UserWarning: The Cython magic has been moved to the Cython package
warnings.warn(“”“The Cython magic has been moved to the Cython package”“”)

I think something has been changed since you wrote the guide. The code still works. I just get this warning the first time.

cython                    0.23.4                   py27_1 
notebook                  4.0.6                    py27_0 

Thanks Bill - indeed cythonmagic has moved to %load_ext Cython for the latest Jupyter notebook.

Fixed - should be on the live site within 24-48 hours.
Thanks.

I hope it is fine to resurrect this topic.

It seems that contrary to usual Python slicing for array types (which took a bit of getting used to) , Pandas’ own indexing functions do select the end point (e.g. the last row). An example in the lectures can be found here:

https://lectures.quantecon.org/py/pandas.html#dataframes

Compare the code following “We can select particular rows using standard Python array slicing notation” with what follows “To select a mix of both…”.

Of course this is not an erratum, but maybe for beginners (like me) it would be good to point out this behavior explicitly?

Best wishes,

Sebastiaan.

Hey Sebastiaan, thanks for the feedback. @mamckay is our pandas expert. @mamckay what do you think?

Hi Sebastiaan. Thanks for your feedback. You make a good point.

We will actually need to do a review of this lecture soon regarding the .ix operator as Pandas is looking to deprecate this way of indexing in favour of .iloc and .loc in version 0.20. I understand that the .ix operator has been a source of bugs for pandas users and they no longer want to support it.

https://github.com/pandas-dev/pandas/issues/14218

I believe .iloc[2:5] would operate as you would expect, which is in line with python slicing.

Thank you both for your replies. The link to the GitHub discussion is quite useful, because starting here it is also addressed how one would use mixed indexing without ix. From that I get that instead of

df.ix[2:5, ['country', 'tcgdp']]

one could write

df.loc[df.index[2:5], ['country', 'tcgdp']]

to get the expected behavior: 5 - 2 = 3 rows with row 5 not included. (The alternative via df.iloc may not work so well with multiple columns because the df.columns.get_loc method apparently does not seem to accept lists, but I may be wrong here.)

In any case, thank you again for the informative link.