codeant-ai-for-open-source[bot] commented on code in PR #42609:
URL: https://github.com/apache/superset/pull/42609#discussion_r3943106056


##########
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:
   ✅ **Customized review instruction saved!**
   
   **Instruction:**
   > Do not flag the leading bucket as incorrect when it is the normal 
start_day bucket, padding rows are empty, and real data is not mislabeled; 
preserve the trailing end-minus-1ns behavior.
   
   **Applied to:**
     - `superset/utils/pandas_postprocessing/resample.py`
   
   ---
   💡 *To manage or update this instruction, visit: [CodeAnt AI 
Settings](https://app.codeant.ai/org/settings/learnings)*



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to