[Bug]: DataFrame mean() and std() fail on datetime and timedelta columns

Aperta
#41,947 1 commento 0 reazioni 1 assegnatario Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

@anishmehta24 ci sta già lavorando.

Dal 4/12/2026.

  • #41948 di @anishmehta24 — aperta

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

P2 python
What happened?

mean() and std() on a deferred Series of datetime64 or timedelta64 values fail. pandas returns a Timestamp or a Timedelta:

import pandas as pd
import apache_beam as beam
from apache_beam.dataframe.transforms import DataframeTransform

df = pd.DataFrame({
    'wait': pd.to_timedelta([10, 20, 60], unit='s'),
    'at': pd.to_datetime(['2024-01-01', '2024-01-02', '2024-01-06']),
})

with beam.Pipeline() as p:
  _ = (
      p | beam.Create([df])
      | DataframeTransform(
          lambda df: df.wait.mean(), proxy=df.iloc[:0], yield_elements='pandas')
      | beam.Map(print))
# pandas gives Timedelta('0 days 00:00:30'), Beam raises ZeroDivisionError

df['at'].mean() raises TypeError: 'DatetimeArray' with dtype datetime64[ns] does not support reduction 'sum', and std() on either column raises TypeError: unsupported operand type(s) for ** or pow(): 'NaTType' and 'int'. Tz-aware datetimes and DataFrame.mean/std(numeric_only=False) fail the same way. Seen on 2.77.0 and on master.

DeferredSeries.mean divides sum() by count(), which can't work for datetimes and fails on the empty timedelta proxy (Timedelta(0) / 0). std goes through var, which squares the result of pandas std, a Timedelta here. pandas computes both on the values as floats in the unit of the dtype and converts the result back.

Issue Priority

Priority: 2 (default / most bugs should be filed as P2)

Issue Components
  • Component: Python SDK
Lingua principale
Java
Stelle
8.7k
Fork
4.8k
Merge medio
3g 20h
PR unite (30g)
446

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di apache/beam

Tutte le issue di apache/beam

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.