Skip to content

TypeError with NumPy 2.4+ (Strict scalar conversion failure) #301

@durr1602

Description

@durr1602

Hi all! I imagine this won't get much attention but, let's say for future reference, if the project gets reborn from its ashes..

The whole Pandas 2.0+ had some important changes, which meant we needed to use different methods to manipulate dataframes. I'm currently using this fork, which was super useful to get rid of all the future warnings.

Now, a similar wave of important changes are happening with Numpy 2.4, notably with scalar casting which has become stricter and will raise bugs with the fork I'm using, but I'm guessing this repo as well.

In my case, calling plt.savefig(..., format="svg") fails with: TypeError: only 0-dimensional arrays can be converted to Python scalars (or ValueError: setting an array element with a sequence). This is with Numpy 2.4.0 but the bug isn't there with Numpy 2.3.5.

Gemini (I know) says "Some internal calculations in upsetplot (likely related to bar labels or category positioning) are passing 1-item numpy.ndarray objects as coordinates to Matplotlib text elements instead of raw Python floats. While the PNG renderer is often forgiving, the SVG renderer explicitly calls float() on coordinates, which now triggers this failure."

Suggested fix:

# Instead of 
ax.text(x, y, label) 
# Use
ax.text(float(np.ravel(x)[0]), float(np.ravel(y)[0]), label)

Finally, I should note that I'm using Matplotlib 3.10.3 but Matplotlib considers that it should fail if numpy.ndarray are passed, so it's upsetplot's responsibility to cast, taking into account Numpy's new behavior.

For my needs, I think I'll downgrade Numpy for now.

Have a nice day!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions