wesm / pydata-book

Materials and IPython notebooks for "Python for Data Analysis" by Wes McKinney, published by O'Reilly Media
Other
22.19k stars 15.17k forks source link

added and fixed demean_axis in appa #178

Open BuWeiSU opened 1 year ago

BuWeiSU commented 1 year ago

I found the code in appa.ipynb don't have function demean_axis in Appendices A, so I added it. When I used numpy==1.23.0 to run demean_axis, an IndexError is raised.

[<ipython-input-1-037e683df74f>](https://localhost:8080/#) in demean_axis(arr, axis)
      6     indexer = [slice(None)] * arr.ndim
      7     indexer[axis] = np.newaxis
----> 8     return arr - means[indexer]

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

When using numpy==1.22.4 or lower, the code can run successfully but a future warning will be displayed.

FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use arr[tuple(seq)] instead of arr[seq]. In the future this will be interpreted as an array index, arr[np.array(seq)], which will result either in an error or a different result.

To fix this error, I added tuple in the last line of demean_axis. By using tuple as index, this error is fixed and the new version is compatible with older numpy versions.