pythonpandasdatetimeindexresample

ValueError: Invalid frequency when resampling


I want to resample a temporal series index with pandas resample. In my case I divide to total period by the amount of intervals to get the step. Then sometimes I get the error:

ValueError: Invalid frequency: 65.117651L

Below I show the index and the error. You can reproduce the error:

pd.DataFrame(index=index).resample('65.117651L')
...
ValueError: Invalid frequency: 65.117651L

Should I modify the frequency a bit the ValueError disapears.

Any idea?

The index is:

index = DatetimeIndex(['2023-09-14 10:41:11.816999936',
               '2023-09-14 10:41:12.002000128',
               '2023-09-14 10:41:12.101999872',
               '2023-09-14 10:41:12.112999936',
               '2023-09-14 10:41:12.210000128',
               '2023-09-14 10:41:12.308999936',
               '2023-09-14 10:41:12.311000064',
               '2023-09-14 10:41:12.408999936',
                  '2023-09-14 10:41:12.424000',
               '2023-09-14 10:41:12.523000064',
                  '2023-09-14 10:41:12.828000',
               '2023-09-14 10:41:12.829999872',
                  '2023-09-14 10:41:12.924000'],
              dtype='datetime64[ns]', name='TS', freq=None)

Solution

  • You're already using nanoseconds, so you can use N modifier:

    df = pd.DataFrame(index=index).resample("65117651N")
    print(df)
    

    Prints:

    DatetimeIndexResampler [freq=<65117651 * Nanos>, axis=0, closed=left, label=left, convention=start, origin=start_day]