{"record":{"id":"53d91599048f3811","repo":"pandas-dev/pandas","slug":"you-must-pass-a-freq-argument-as-current-index-has","errorCode":null,"errorMessage":"You must pass a freq argument as current index has none.","messagePattern":"You must pass a freq argument as current index has none\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/datetimes.py","lineNumber":1276,"sourceCode":"        >>> idx.to_period()\n        PeriodIndex(['2017-01-01', '2017-01-02'],\n                    dtype='period[D]')\n        \"\"\"\n        from pandas.core.arrays import PeriodArray\n\n        if self.tz is not None:\n            warnings.warn(\n                \"Converting to PeriodArray/Index representation \"\n                \"will drop timezone information.\",\n                UserWarning,\n                stacklevel=find_stack_level(),\n            )\n\n        if freq is None:\n            freq = self._inferred_freq_str\n\n            if freq is None:\n                raise ValueError(\n                    \"You must pass a freq argument as current index has none.\"\n                )\n\n            res = get_period_alias(freq)\n\n            #  https://github.com/pandas-dev/pandas/issues/33358\n            if res is None:\n                res = freq\n\n            freq = res\n        return PeriodArray._from_datetime64(self._ndarray, freq, tz=self.tz)\n\n    # -----------------------------------------------------------------\n    # Properties - Vectorized Timestamp Properties/Methods\n\n    def month_name(self, locale=None) -> npt.NDArray[np.object_]:\n        \"\"\"\n        Return the month names with specified locale.","sourceCodeStart":1258,"sourceCodeEnd":1294,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/datetimes.py#L1258-L1294","documentation":"Raised by DatetimeArray.to_period when no `freq` is passed and the index has no inferable frequency (self._inferred_freq_str is None). Conversion to PeriodArray requires a period frequency; if the DatetimeIndex is irregular (gaps, duplicates, unordered), pandas cannot guess one.","triggerScenarios":"Calling `dti.to_period()` on an irregular DatetimeIndex (filtered, resampled-but-not-asfreq, manually constructed, or containing duplicates/NaT). Calling `.to_period()` on a Series.dt accessor whose underlying index is irregular.","commonSituations":"Filtering rows then `.to_period('M')` works (freq explicit) but `.to_period()` without an arg fails because the filtered index lost its regular cadence. Reading real-world timestamps that have jitter or missing rows.","solutions":["Pass an explicit freq: `dti.to_period('D')`, `dti.to_period('M')`, etc.","Re-establish a regular index first: `df.asfreq('D')` or `df.resample('D').asfreq()` before to_period.","Drop duplicates and sort: `dti = dti.drop_duplicates().sort_values()` then retry.","If you only want period-like grouping, use `.dt.to_period('M')` on a column rather than the index."],"exampleFix":"// before\nperiods = irregular_dti.to_period()\n\n// after\nperiods = irregular_dti.to_period('D')","handlingStrategy":"validation","validationCode":"import pandas as pd\n\ndef safe_to_period(dti, freq=None):\n    freq = freq or dti.freq\n    if freq is None:\n        freq = 'D'\n    return dti.to_period(freq=freq)","typeGuard":"def has_inferable_freq(dti) -> bool:\n    return dti.freq is not None or dti.inferred_freq is not None","tryCatchPattern":"try:\n    p = dti.to_period()\nexcept ValueError as e:\n    if 'freq argument' in str(e):\n        p = dti.to_period('D')\n    else:\n        raise","preventionTips":["Always pass an explicit freq to to_period in production code.","Re-establish regularity with asfreq/resample before to_period when the index is filtered."],"tags":["datetime","period","frequency","to-period"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}