Skip to content

BUG: inconsistent median support for datetimelike dtypes #33760

Closed
@jorisvandenbossche

Description

@jorisvandenbossche

#29941 introduced support for taking the median of a datetime-like column.
In released version, (pandas 1.0.3) we get:

In [20]: df = pd.DataFrame({'a': pd.date_range("2012", periods=3, freq='D')})  

In [21]: df.median()    
Out[21]: Series([], dtype: float64)

In [22]: df.median(numeric_only=False)  
...
TypeError: reduction operation 'median' not allowed for this dtype

In [23]: df['a'].median()  
...
TypeError: DatetimeIndex cannot perform the operation median

on master we now allow this for DataFrame (warning it will be in the future by default, and already allow it with numeric_only=False):

In [2]: df.median()  
/home/joris/miniconda3/envs/dev/bin/ipython:1: FutureWarning: DataFrame.mean and DataFrame.median with numeric_only=None will include datetime64, datetime64tz, and PeriodDtype columns in a future version.
  #!/home/joris/miniconda3/envs/dev/bin/python
Out[2]: Series([], dtype: float64)

In [3]: df.median(numeric_only=False)  
Out[3]: 
a    2012-01-02 00:00:00
dtype: object

but for a single column it still fails:

In [4]: df['a'].median()  
...
TypeError: cannot perform median with type datetime64[ns]

So we should make this support consistent (add "median" support on DatetimeArray, so it also works for Series)

cc @jbrockmendel

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions