Skip to content

numpy/char-module

Pack Severity Fix Versions
NumPy info unsafe, with xpd check --fix --unsafe-fixes NumPy 2.0+

numpy.char is legacy, and NumPy's docs point to numpy.strings. Call the function of the same name in np.strings.

names = np.char.upper(names)
names = np.strings.upper(names)

The rule reports a function of np.char when np.strings, added in NumPy 2.0, has a function of the same name. NumPy's docs call np.char legacy.

xpd check --fix --unsafe-fixes applies the fixes of this rule. Check the result, because an unsafe fix can change what the code does.

The safe fix writes np.strings.upper and every other name that is the same function in both modules. Unsafe: the comparisons, such as np.strings.equal, which do not strip trailing whitespace, and np.strings.multiply, which raises a different error on bad input. Unsafe also: partition and rpartition, because np.strings returns a tuple of three arrays.

The comparison rewrite can also change behavior on bytes and object or StringDType arrays. A scalar comparison can return a bool in place of a zero-dimensional array, and the multiplication rewrite can raise a different exception.

numpy.char is legacy, and NumPy's docs say to use numpy.strings. Write $NP.strings.$F.

See all rules in the NumPy pack.