{"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":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/datetimes.py","lineNumber":1278,"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":1260,"sourceCodeEnd":1296,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/datetimes.py#L1260-L1296","documentation":"Raised by DatetimeIndex.to_period when neither an explicit freq nor an inferrable freq exists on the index. to_period needs a frequency to map each timestamp to a Period; without one the mapping is undefined. ValueError.","triggerScenarios":"Calling .to_period() on an irregular DatetimeIndex (timestamps from real events, gaps, duplicates) with no freq= passed; index created from arbitrary datetime lists rather than date_range.","commonSituations":"Resampling/grouping by period on messy timestamps; converting an event-log index to periods without first ascertaining a freq.","solutions":["Pass an explicit freq: idx.to_period('D') or .to_period(freq='h').","If the index should be regular, rebuild it with pd.date_range(..., freq=...) or set a freq via idx.asfreq / idx.inferred_freq.","For irregular data, group with .resample('D') or .dt.to_period('D') on the column instead of the index."],"exampleFix":"# before\nidx.to_period()\n# after\nidx.to_period('D')","handlingStrategy":"validation","validationCode":"def safe_to_period(idx, freq=None):\n    freq = freq or idx.freq or idx.inferred_freq\n    if freq is None:\n        raise ValueError('index has no freq; pass freq= explicitly')\n    return idx.to_period(freq)","typeGuard":"def has_freq(idx) -> bool:\n    return getattr(idx, 'freq', None) is not None or idx.inferred_freq is not None","tryCatchPattern":null,"preventionTips":["Always pass an explicit freq to to_period for irregular indexes.","Build indexes with pd.date_range(freq=...) when periodicity is expected.","Use .resample for grouping irregular data."],"tags":["datetime","period","frequency","pandas"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}