pandas-dev/pandas · error · ValueError
Inferred frequency from passed values does not conform to…
Error message
Inferred frequency {inferred} from passed values does not conform to passed frequency {freq.freqstr} What it means
Raised by _validate_frequency when constructing a DatetimeIndex/TimedeltaIndex with an explicit freq= argument whose step does not match what pandas infers from the supplied values (GH#11587). The class generates a range at the declared freq from index[0] and compares asi8 byte arrays; on mismatch (or when index[0] is NaT) it re-raises this targeted message.
Solutions
- Omit freq= and let pandas infer it, then optionally call .asfreq(desired) to enforce.
- Clean the source values so they actually match the desired grid before passing freq=.
- If the data is meant to be irregular, drop the freq argument entirely.
- Set freq=None on an existing index via idx = idx._with_freq(None) before reuse if you only need the values.
Example fix
// before
idx = pd.DatetimeIndex(['2020-01-01','2020-01-03'], freq='D') # ValueError
// after
idx = pd.DatetimeIndex(['2020-01-01','2020-01-03']) # inferred freq=None
# or, to enforce daily:
idx = pd.DatetimeIndex(['2020-01-01','2020-01-02']).asfreq('D') Defensive patterns
Strategy: validation
Validate before calling
def make_index(values, freq=None):
if freq is None:
return pd.DatetimeIndex(values)
inferred = pd.DatetimeIndex(values).inferred_freq
if inferred is not None and inferred != pd.tseries.frequencies.to_offset(freq).freqstr:
return pd.DatetimeIndex(values) # let pandas infer; do not force freq
return pd.DatetimeIndex(values, freq=freq) Type guard
def freq_matches(values, freq) -> bool:
inferred = pd.DatetimeIndex(values).inferred_freq
return inferred is not None and inferred == pd.tseries.frequencies.to_offset(freq).freqstr Try / catch
try:
idx = pd.DatetimeIndex(values, freq=freq)
except ValueError as e:
if "does not conform to passed frequency" in str(e):
idx = pd.DatetimeIndex(values) # fall back to inferred/no freq
else:
raise Prevention
- After subsetting or filtering a DatetimeIndex, drop its freq: idx = idx._with_freq(None).
- Prefer .asfreq(desired) over passing freq= at construction when enforcement is the goal.
- Validate inferred_freq equals the intended offset before passing freq=.
When it happens
Trigger: pd.DatetimeIndex(values, freq='D') where values are not actually daily-spaced; pd.date_range validation paths that pass both data and a freq; round-tripping data that was filtered/missing rows but retained a stale freq attribute.
Common situations: Hand-editing or subsetting a DatetimeIndex without dropping its freq; concatenating indexes whose spacing differs; loading data with gaps but asserting a business-day or fixed frequency; timezone edge cases for non-Tick offsets.
Related errors
- Must provide freq argument if no data is supplied
- cannot assign without a target object
- Cannot create a from a MultiIndex.
- Cannot directly set timezone. Use tz_localize() or…
- does not have a resolution.
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/99d17d98c0f951de.
Report an issue: GitHub.
Appendix: source
Thrown at pandas/core/arrays/datetimelike.py:1880
start=index[0],
end=None,
periods=len(index),
freq=freq,
unit=index.unit,
)
if not lib.array_equivalent_bytes(index.asi8, on_freq.asi8):
raise ValueError
except ValueError as err:
if "non-fixed" in str(err):
# non-fixed frequencies are not meaningful for timedelta64;
# we retain that error message
raise err
# GH#11587 the main way this is reached is if the `np.array_equal`
# check above is False. This can also be reached if index[0]
# is `NaT`, in which case the call to `cls._generate_range` will
# raise a ValueError, which we re-raise with a more targeted
# message.
raise ValueError(
f"Inferred frequency {inferred} from passed values "
f"does not conform to passed frequency {freq.freqstr}"
) from err
@classmethod
def _generate_range(
cls, start, end, periods: int | None, freq, *args, **kwargs
) -> Self:
raise AbstractMethodError(cls)
# --------------------------------------------------------------
@cache_readonly
def _creso(self) -> int:
return get_unit_from_dtype(self._ndarray.dtype)
@cache_readonly
def unit(self) -> TimeUnit:View on GitHub (pinned to 3b7651241d)