Skip to content

BUG: inconsistent median support for datetimelike dtypes #33760

@jorisvandenbossche

Description

@jorisvandenbossche
Member

#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

Activity

added this to the 1.1 milestone on Apr 24, 2020
jbrockmendel

jbrockmendel commented on Apr 24, 2020

@jbrockmendel
Member

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

+1

modified the milestones: 1.1, Contributions Welcome on Jul 10, 2020
modified the milestones: Contributions Welcome, 1.2 on Oct 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      Participants

      @jreback@jorisvandenbossche@jbrockmendel@mroeschke

      Issue actions

        BUG: inconsistent median support for datetimelike dtypes · Issue #33760 · pandas-dev/pandas