Getting TypeError: reduction operation ‘argmax’ not allowed for this dtype when trying to use idxmax()

The type of the cell values are, by default, non-numeric. argmin(), idxmin(), argmax() and other similar functions need the dtypes to be numeric. The easiest solution is to use pd.to_numeric() in order to convert your series (or columns) to numeric types. An example with a data frame df with a column ‘a’ would be: df[‘a’] … Read more

Why is str.strip() so much faster than str.strip(‘ ‘)?

In a tl;dr fashion: This is because two functions exist for the two different cases, as can be seen in unicode_strip; do_strip and _PyUnicodeXStrip the first executing much faster than the second. Function do_strip is for the common case str.strip() where no arguments exist and do_argstrip (which wraps _PyUnicode_XStrip) for the case where str.strip(arg) is … Read more

execute a Jupyter Notebook cell programmatically

There is a javascript function called execute_cells (see it on Github) that when given an list of cell indices runs those cells. %%javascript Jupyter.notebook.execute_cells([0]) # 0 to run first cell in notebook etc. If you need to run it specifically in a Python code cell, one can use the Javascript function in the IPython.display module … Read more

How to convert Matplotlib figure to PIL Image object (without saving image)

EDIT # 2 PIL.Image.frombytes(‘RGB’, fig.canvas.get_width_height(),fig.canvas.tostring_rgb()) takes around 2ms compared to the 35/40ms of the below. This is the fastest way I can find so far. I’ve been looking at this also today. In the matplotlib docs the savefig function had this. pil_kwargsdict, optional Additional keyword arguments that are passed to PIL.Image.save when saving the figure. … Read more

Unable to use pypdf module

This is a problem of an old version of pypdf. The history of pypdf is a bit compliated, but the gist of it: Use pypdf>=3.1.0. All lowercase, no number. Since December 2022, it’s the best supported version. Install pypdf $ sudo -H pip install pypdf You might need to replace pip by pip2 or pip3 … Read more