PythonDataScienceHandbook icon indicating copy to clipboard operation
PythonDataScienceHandbook copied to clipboard

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

Open milodubois opened this issue 3 years ago • 0 comments

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

milodubois avatar Sep 20 '22 11:09 milodubois