Skip to content

pandas/astype-then-to-numpy

Pack Severity Fix Versions
pandas warning unsafe, with xpd check --fix --unsafe-fixes All versions

astype builds a whole Series only for to_numpy to take its values. Pass the dtype to to_numpy instead.

limit = rows["Economic Maximum"].astype(float).to_numpy()
limit = rows["Economic Maximum"].to_numpy(dtype=float)

The rule reports s.astype(float).to_numpy().

It leaves a text dtype alone, such as s.astype(str).to_numpy(), because the two forms differ for it. On pandas 3.0.5 astype(str).to_numpy() gave an object array and to_numpy(dtype=str) a fixed-width one, and a datetime column gave "2025-01-01" with the first and "2025-01-01T00:00:00.000000" with the second. Use care with the string alias as well. to_numpy(dtype="string") raised TypeError in the same comparison.

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 unsafe fix writes s.to_numpy(dtype=float), because a missing value in a nullable column can convert in another way.

xpd applies the replacement with --fix --unsafe-fixes.

astype builds a second Series with the new dtype only for to_numpy to take its values. to_numpy takes the dtype itself. On pandas 3.1.1 both forms gave an equal float64 array for an int column.

astype builds a Series only to take its values. Pass the dtype to to_numpy, as in $X.to_numpy(dtype=$T).

See all rules in the pandas pack.