anishmehta24 opened a new issue, #41947:
URL: https://github.com/apache/beam/issues/41947

   ### What happened?
   
   `mean()` and `std()` on a deferred Series of `datetime64` or `timedelta64` 
values fail. pandas returns a Timestamp or a Timedelta:
   
   ```python
   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
   
   - [x] Component: Python SDK
   


-- 
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]

Reply via email to