Skip to content

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

Closed

Description

@longye-tian

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.

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

These operations are still valid.

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

This is still valid.

  1. Interpolation:
gdp_pc[country].interpolate()

This method is still supported.

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

This plotting method is still valid.

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

This is still supported.

  1. DateTime conversion:
regionalgdp_pc.index = pd.to_datetime(regionalgdp_pc.index, format='%Y')

This is still valid.

Best ❤️
Longye

Activity

  1. mmcky commented on Jun 21, 2024

    @mmcky
    Contributor

    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.

  2. mmcky commented on Jul 1, 2024

    @mmcky
    Contributor

    thanks again @longye-tian for reviewing the pandas code. Appreciate the review. This issue can now be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions