numpy/numpy · error · ValueError
Cannot specify both "C" and "F" order
Error message
Cannot specify both "C" and "F" order
What it means
`np.require(a, requirements=...)` validates the requirements list against a fixed flag set. The flags 'C' (C-contiguous) and 'F' (Fortran-contiguous) are mutually exclusive memory layouts, so requesting both simultaneously is impossible for any non-trivial array. NumPy raises ValueError rather than silently picking one.
Source
Thrown at numpy/_core/_asarray.py:114
a,
dtype=dtype,
requirements=requirements,
)
if not requirements:
return asanyarray(a, dtype=dtype)
requirements = {POSSIBLE_FLAGS[x.upper()] for x in requirements}
if 'E' in requirements:
requirements.remove('E')
subok = False
else:
subok = True
order = 'A'
if requirements >= {'C', 'F'}:
raise ValueError('Cannot specify both "C" and "F" order')
elif 'F' in requirements:
order = 'F'
requirements.remove('F')
elif 'C' in requirements:
order = 'C'
requirements.remove('C')
arr = array(a, dtype=dtype, order=order, copy=None, subok=subok)
for prop in requirements:
if not arr.flags[prop]:
return arr.copy(order)
return arr
_require_with_like = array_function_dispatch()(require)
View on GitHub (pinned to e117b3ca4e)
Solutions
- Choose one layout: requirements=['C'] for C-contiguous or requirements=['F'] for Fortran-contiguous.
- If you need neither specifically, use 'A' (any) or omit the contiguity requirement.
- Validate the requirements set before calling: assert not ({'C','F'} <= set(req)).
Example fix
// before arr = np.require(a, requirements=['C','F','W']) # ValueError // after arr = np.require(a, requirements=['C','W'])
Defensive patterns
Strategy: validation
Validate before calling
import numpy as np
req = {'C','W'}
assert not ({'C','F'} <= req), 'C and F contiguity are mutually exclusive'
arr = np.require(a, requirements=list(req)) Type guard
def valid_requirements(req) -> bool:
s = set(req)
return not ({'C','F'} <= s) Try / catch
try:
arr = np.require(a, requirements=req)
except ValueError as e:
if 'C" and "F' in str(e):
req = [r for r in req if r not in ('F','F_CONTIGUOUS')]
arr = np.require(a, requirements=req)
else:
raise Prevention
- Never combine C and F contiguity in requirements.
- Build requirements from a validated flag set.
- Document memory-layout intent explicitly in the call.
When it happens
Trigger: Calling `np.require(a, requirements=['C','F'])`, `np.require(a, requirements=['C_CONTIGUOUS','F_CONTIGUOUS'])`, or any combination normalizing to both C and F (including passing a set/superset like {'C','F','W'}).
Common situations: Building a requirements list programmatically that accidentally includes both flags; misunderstanding that 'C' and 'F' are exclusive; copying requirements from two different call sites.
Related errors
- Device not understood. Only "cpu" is allowed, but received:
- unsupported kind: {kind!r}
- entry not a 2- or 3- tuple
- invalid offset.
- format number {len(result) + 1} of "{astr}" is not recognize
AI-assisted analysis of numpy/numpy@e117b3ca4e (2026-08-07).
Data as JSON: /api/errors/0108a813f5f1ad04.
Report an issue: GitHub.