Skip to content

[preprocessing] fill_gaps with pandas drops every value when times aren't at the start of the frequency's unit (non-midnight daily, fractional-offset hourly) #329

Open

Description

What happened + What you expected to happen

With pandas dataframes, fill_gaps returns every original value as NaN (only a "Some values were lost during filling" warning) whenever the times aren't at the start of the frequency's numpy unit. The same data as a polars dataframe is filled correctly. Cases that hit this:

  1. Daily data at a fixed time other than midnight (e.g. 09:00), naive or timezone-aware.
  2. Hourly data in zones with a fractional UTC offset (e.g. Asia/Kolkata, Asia/Kathmandu), since the grid is built in UTC where those times are at :30 / :15.
  3. Frames where series are aligned differently (e.g. one series at 09:00 and another at midnight): one of them loses every value, depending on which comes first in the frame.
  4. With fix: fill_gaps with pandas drops every value for timezone-aware daily or coarser data #328: a daily series whose first row falls on a day whose local midnight doesn't exist because of DST (e.g. America/Santiago 2024-09-08, which starts at 01:00) loses that first value.

Expected: the filled times match the input's alignment and only the missing periods are NaN, as with polars.

Cause: in utilsforecast/preprocessing.py, the pandas path of id_time_grid floors every start and end to the numpy unit (astype(f"datetime64[{freq}]")), then decides whether to shift the whole grid by offset.base from df.iloc[0] alone (was_truncated). Times that aren't on the unit boundary are moved to the next boundary instead of kept, and the decision made from the first row is applied to every series.

Versions / Dependencies

Click to expand Dependencies:

utilsforecast main (739fbf5, 0.2.17), pandas 2.3.3, polars 1.31.0, Python 3.10, Linux.

Reproducible example

import warnings

import numpy as np
import pandas as pd
import polars as pl

from utilsforecast.preprocessing import fill_gaps

warnings.simplefilter("ignore")


def show(name, df, freq, pl_freq):
    df = df.drop(index=2).reset_index(drop=True)
    res = fill_gaps(df, freq)
    res_pl = fill_gaps(pl.from_pandas(df), pl_freq)
    print(
        f"{name}: pandas kept {res['y'].count()}/{len(df)}, first ds={res['ds'].iloc[0]} "
        f"| polars kept {res_pl['y'].count()}/{len(df)}"
    )


for tz in [None, "America/New_York"]:
    d = pd.date_range("2024-01-01 09:00", periods=10, freq="D", tz=tz)
    show(f"daily 09:00 tz={tz}", pd.DataFrame({"unique_id": "a", "ds": d, "y": np.arange(10.0)}), "D", "1d")
for tz in ["Asia/Kolkata", "Asia/Kathmandu"]:
    d = pd.date_range("2024-01-01", periods=10, freq="h", tz=tz)
    show(f"hourly {tz}", pd.DataFrame({"unique_id": "a", "ds": d, "y": np.arange(10.0)}), "h", "1h")
a = pd.date_range("2024-01-01 09:00", periods=5, freq="D")
b = pd.date_range("2024-01-01", periods=5, freq="D")
df = pd.DataFrame({"unique_id": ["a"] * 5 + ["b"] * 5, "ds": a.append(b), "y": np.arange(10.0)})
show("series a at 09:00, b at midnight", df, "D", "1d")

Output on main:

daily 09:00 tz=None: pandas kept 0/9, first ds=2024-01-02 00:00:00 | polars kept 9/9
daily 09:00 tz=America/New_York: pandas kept 0/9, first ds=2024-01-01 19:00:00-05:00 | polars kept 9/9
hourly Asia/Kolkata: pandas kept 0/9, first ds=2024-01-01 00:30:00+05:30 | polars kept 9/9
hourly Asia/Kathmandu: pandas kept 0/9, first ds=2024-01-01 00:45:00+05:45 | polars kept 9/9
series a at 09:00, b at midnight: pandas kept 4/9, first ds=2024-01-02 00:00:00 | polars kept 9/9

The results are the same with #328 applied, except that the tz-aware daily grid starts at 2024-01-02 00:00:00-05:00.

Issue Severity

Medium: It is a significant difficulty but I can work around it.

Activity

  1. added
    bug
    on Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions