jakevdp / PythonDataScienceHandbook

Python Data Science Handbook: full text in Jupyter Notebooks
http://jakevdp.github.io/PythonDataScienceHandbook
MIT License
43.12k stars 17.91k forks source link

Update deprecated `df.ix[]` to `df.loc[]` in '03.11-working-with-time-series.html' #350

Closed milodubois closed 2 years ago

milodubois commented 2 years ago

pandas.DataFrame.ix is deprecated as of version 0.20.0, and consequently the following code from https://jakevdp.github.io/PythonDataScienceHandbook/03.11-working-with-time-series.html generates error if alternative is not used (described in second code block).

incorrect (uses deprecated indexer):

import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 2, figsize=(14, 5))
by_time.ix['Weekday'].plot(ax=ax[0], title='Weekdays',
                           xticks=hourly_ticks, style=[':', '--', '-'])
by_time.ix['Weekend'].plot(ax=ax[1], title='Weekends',
                           xticks=hourly_ticks, style=[':', '--', '-']);

correct (uses loc):

import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 2, figsize=(14, 5))
by_time.loc['Weekday'].plot(ax=ax[0], title='Weekdays',
                           xticks=hourly_ticks, style=[':', '--', '-'])
by_time.loc['Weekend'].plot(ax=ax[1], title='Weekends',
                           xticks=hourly_ticks, style=[':', '--', '-']);

Kindly, N.B., loc should be used in place of ix. Using the code prefixed by 'correct' will generate the appropriate plot as described in the chapter.

Regards, Milo

jakevdp commented 2 years ago

Thanks – see https://www.oreilly.com/library/view/python-data-science/9781098121211/; I'll update the notebooks here soon.