You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by "rusackas (via GitHub)" <gi...@apache.org> on 2026/09/06 05:41:56 UTC

Re: [PR] feat(resample): Add "Fill the entire time range" option to Resample controls (#42587) [superset]

rusackas commented on code in PR #42609:
URL: https://github.com/apache/superset/pull/42609#discussion_r3943105379


##########
superset/utils/pandas_postprocessing/resample.py:
##########
@@ -22,12 +23,83 @@
 from superset.exceptions import InvalidPostProcessingError
 from superset.utils.pandas_postprocessing.utils import RESAMPLE_METHOD
 
+TimeBound = Union[datetime, str]
 
-def resample(
+
+def _coerce_bound(
+    value: Optional[TimeBound], tz: Optional[tzinfo]
+) -> Optional[pd.Timestamp]:
+    """
+    Normalize a time range boundary into a ``Timestamp`` comparable with the index.
+
+    :param value: Boundary as a datetime or a parseable string.
+    :param tz: Timezone of the DataFrame index, if any.
+    :return: Timestamp aligned with the index timezone awareness, or None.
+    :raises InvalidPostProcessingError: If the boundary cannot be parsed.
+    """
+    if value is None:
+        return None
+    try:
+        timestamp = pd.Timestamp(value)
+    except (TypeError, ValueError) as ex:
+        raise InvalidPostProcessingError(
+            _("Invalid time range boundary for resample: %(value)s", value=value)
+        ) from ex
+
+    if timestamp.tzinfo is None:
+        return timestamp if tz is None else timestamp.tz_localize(tz)
+    # an index and a boundary in different timezones would append into an
+    # object-dtype index that ``resample`` cannot bin
+    return timestamp.tz_localize(None) if tz is None else timestamp.tz_convert(tz)
+
+
+def _pad_to_time_range(
+    df: pd.DataFrame,
+    time_range_start: Optional[pd.Timestamp],
+    time_range_end: Optional[pd.Timestamp],
+) -> pd.DataFrame:
+    """
+    Add empty rows at the edges of the target period.
+
+    ``DataFrame.resample`` derives its bins from the first and last index entries,
+    so a series that only covers part of the requested time range is only filled
+    between its own extremes. Anchoring the index to the boundaries of the period
+    makes pandas emit buckets for the whole period instead.
+
+    :param df: DataFrame with a DatetimeIndex.
+    :param time_range_start: Inclusive lower boundary of the period.
+    :param time_range_end: Exclusive upper boundary of the period.
+    :return: DataFrame whose index spans the target period.
+    """
+    index = df.index
+    anchors = []
+
+    if time_range_start is not None and (index.empty or time_range_start < index.min()):
+        anchors.append(time_range_start)
+
+    if time_range_end is not None:
+        # the upper boundary of a Superset time range is exclusive, so anchor on
+        # the last instant that still belongs to the period
+        last_instant = time_range_end - pd.Timedelta(1, unit="ns")
+        if index.empty or last_instant > index.max():
+            anchors.append(last_instant)

Review Comment:
   Checked this against the current head in pandas. The leading 10:00 label is just the normal start_day bucketing, the same bucket a real 10:45 row would land in (and the same one the time grain would give it), and the padding rows are empty so no real data gets mislabeled. The trailing edge is exact thanks to the end minus 1ns anchor. Amin reached the same read in his review, so resolving this one.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org