Skip to main content
v2026.11,986 entries · CC-BY 4.0

NumPy Power: np.power Syntax, Errors and dtype

How np.power works: syntax, broadcasting, and the documented fixes for ValueError, nan and integer overflow, plus np.float_power and the ** operator.

Written and maintained by CASRAI Editorial Board

Published · Last updated

On this page

np.power(x1, x2) raises each element of x1 to the matching power in x2, element by element, and returns an array (or a scalar if both inputs are scalars). It keeps the input type, which is exactly why it fails in three predictable ways: a ValueError for integer bases with negative integer exponents, nan for negative bases with fractional exponents, and silent wrap-around when integers overflow. This guide gives the syntax, then a failure-mode table with the documented fix for each.

What np.power is used for in research

In analysis code, element-wise powers appear when you build squared or cubed terms for a polynomial regression design matrix, apply a power transform to a skewed variable, or rescale an array by a fractional exponent. np.power does this on whole arrays without a Python loop. For the specific case of squaring, see how to square an array in NumPy; for exponent 0.5, see NumPy square root.

Install or access

NumPy is installed from the official NumPy install page. The commands listed there include pip install numpy, conda install numpy, uv pip install numpy and pixi add numpy. Then import it in the usual way:

import numpy as np

The NumPy reference lists numpy.pow with the same signature as numpy.power, shown as the same ufunc, so the two names refer to the same function.

Step-by-step: raise an array to a power with np.power

The signature in the current reference is numpy.power(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True). x1 holds the bases and x2 the exponents; both are array_like and must be broadcastable to a common shape, which becomes the output shape.

  1. Create the bases. x1 = np.arange(6)
  2. Use a scalar exponent. np.power(x1, 3) returns array([ 0, 1, 8, 27, 64, 125]).
  3. Use an array of exponents. With x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0], np.power(x1, x2) returns array([ 0., 1., 8., 27., 16., 5.]). Each base is paired with the exponent in the same position.
  4. Broadcast a 2D exponent array. With x2 = np.array([[1,2,3,3,2,1],[1,2,3,3,2,1]]), np.power(x1, x2) returns two identical rows, [ 0, 1, 8, 27, 16, 5], because x1 is broadcast across the rows.
  5. Optionally use the operator. The reference states that the ** operator can be used as a shorthand for np.power on ndarrays, so x1 ** x2 gives the same result as step 3.

Output and how to read it

The result is a new ndarray unless you pass out; if both inputs are scalars you get a scalar back. If every input is an integer array the output stays integer. As soon as either argument is a float array, the output is float, which is why step 3 prints 16. rather than 16. The where argument limits the computation to positions where it is True; positions where it is False keep the value already in out, and with out=None those positions are left uninitialized, so do not read them.

Troubleshooting

Most problems come from np.power preserving type. Match your symptom to a row:

Symptom Cause (from the NumPy docs) Fix
ValueError An integer type raised to a negative integer power raises a ValueError. Use a float exponent or base (for example 2.0), or use np.float_power, which promotes integers to float.
nan in the output Negative values raised to a non-integral value return nan. np.power(np.array([-1.0, -4.0]), 1.5) gives array([nan, nan]). Cast the input to complex or pass dtype as complex. With dtype=np.complex128 the documented result is array([-1.83697020e-16-1.j, -1.46957616e-15-8.j]).
Wrong or negative integer results Fixed-size integers overflow without an error. In the NumPy data types guide, 100**9 is correct in int64 (1000000000000000000) but gives -1486618624 in int32. Use a wider dtype, or a float dtype when the value is out of integer range.
Result of 0 for a huge power In the same guide, np.power(100, 100, dtype=np.int64) returns 0, which is incorrect, while dtype=np.float64 returns 1e+200. Request dtype=np.float64 (or use np.float_power).

If you only want to silence the invalid-value warning while knowing the nan is expected, the reference example wraps the call in with np.errstate(invalid='ignore'):.

Free alternatives

Option What the docs say
x1 ** x2 Shorthand for np.power on ndarrays, so it has the same type-preserving behavior.
np.float_power(x1, x2) Same signature, but integers, float16 and float32 are promoted to floats with a minimum precision of float64, so the result is always inexact. It is meant to return a usable result for negative powers and seldom overflow for positive powers. Negative bases with non-integral exponents still return nan unless the input or dtype is complex. Example: np.float_power(range(6), 3) gives array([ 0., 1., 8., 27., 64., 125.]).

Both are element-wise. They do not compute matrix powers. For matrix products, see matrix multiplication in NumPy. To generate exponent grids, see NumPy linspace and NumPy logspace.

FAQ

What does np.power do?

It raises the elements of the first array to the powers in the second array, element-wise, with the two inputs broadcast to a common shape.

Is np.pow the same as np.power?

The NumPy reference lists numpy.pow with the same signature, shown as the same ufunc as numpy.power.

Why does np.power raise a ValueError with a negative exponent?

Because an integer type raised to a negative integer power raises a ValueError. Use a float exponent or np.float_power.

Why do I get nan?

Negative values raised to a non-integral value return nan. Cast to complex or set a complex dtype to get complex results.

Is the ** operator the same as np.power?

On ndarrays, the reference says ** can be used as a shorthand for np.power.

Sources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about NumPy Power: np.power Syntax, Errors and dtype

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →