QuantEcon / lecture-python-intro

An Undergraduate Lecture Series for the Foundations of Computational Economics
https://intro.quantecon.org/
34 stars 17 forks source link

[long_run_growth] Review Pandas code, plt.savefig #467

Open longye-tian opened 1 week ago

longye-tian commented 1 week ago

Dear John @jstac and Matt @mmcky ,

After reviewing the lecture long_run_growth.ipynb (related to #350 ), I don't see any deprecated or significantly changed functionality that would require updates related to pandas >=2.0. The code appears to be compatible with recent pandas versions.

In the last block of code in section Early industrialization:

fig, ax = plt.subplots(dpi=300)
country = ['DEU', 'USA', 'SUN', 'BEM', 'FRA', 'JPN']
start_year, end_year = (1821, 1945)
draw_interp_plots(gdp[country].loc[start_year:end_year], 
                  country,
                  'international dollars', 'year',
                  color_mapping, code_to_name, 2, False, ax)

plt.savefig("./_static/lecture_specific/long_run_growth/tooze_ch1_graph.png", dpi=300,
            bbox_inches='tight')
plt.show()

I believe the plt.savefig part may not be necessary. When I run this code in my Jupyter Notebook, it generates an error. Removing this part should still allow the plot to be generated successfully.

Do you think we should change this code?


Here are the details for reviewing the pandas-related code in this lecture.

  1. Reading Excel files:

    data = pd.read_excel(data_url, sheet_name='Full data')

    This is still valid in recent pandas versions.

  2. DataFrame:

    data.set_index(['countrycode', 'year'], inplace=True)
    data['gdp'] = data['gdppc'] * data['pop']
    gdp = data['gdp'].unstack('countrycode')

    These operations are still valid.

  3. DataFrame:

    code_to_name = data[['countrycode', 'country']].drop_duplicates().reset_index(drop=True).set_index(['countrycode'])

    This is still valid.

  4. Interpolation:

    gdp_pc[country].interpolate()

    This method is still supported.

  5. Plotting:

    gdp_pc[country].plot(ax=ax, ylabel='international dollars', xlabel='year', color=color_mapping[country])

    This plotting method is still valid.

  6. Columns

    data.columns = data.columns.droplevel(level=2)

    This is still supported.

  7. DateTime conversion:

    regionalgdp_pc.index = pd.to_datetime(regionalgdp_pc.index, format='%Y')

    This is still valid.

Best ❤️ Longye

mmcky commented 1 week ago

thanks @longye-tian. Very nice review.

I believe the plt.savefig part may not be necessary.

This is saving the figure that we use at the beginning of the lecture using a figure directive. You're right -- it may not be necessary but perhaps we should copy of the code that is used to generate that figure and add it to _static/lecture_specific/long_run_growth in a figures.ipynb notebook.