6
6
7
7
import numpy as np
8
8
from pandas import compat
9
- from pandas ._libs import tslib , algos , lib
9
+ from pandas ._libs import tslib , lib
10
10
from pandas .core .dtypes .common import (
11
11
_get_dtype ,
12
12
is_float , is_scalar ,
@@ -370,14 +370,13 @@ def nanmean(values, axis=None, skipna=True):
370
370
@bottleneck_switch ()
371
371
def nanmedian (values , axis = None , skipna = True ):
372
372
373
- values , mask , dtype , dtype_max = _get_values (values , skipna )
374
-
375
373
def get_median (x ):
376
374
mask = notna (x )
377
375
if not skipna and not mask .all ():
378
376
return np .nan
379
- return algos . median ( com . _values_from_object ( x [mask ]) )
377
+ return np . nanmedian ( x [mask ])
380
378
379
+ values , mask , dtype , dtype_max = _get_values (values , skipna )
381
380
if not is_float_dtype (values ):
382
381
values = values .astype ('f8' )
383
382
values [mask ] = np .nan
@@ -389,10 +388,15 @@ def get_median(x):
389
388
390
389
# an array from a frame
391
390
if values .ndim > 1 :
391
+
392
392
# there's a non-empty array to apply over otherwise numpy raises
393
393
if notempty :
394
- return _wrap_results (
395
- np .apply_along_axis (get_median , axis , values ), dtype )
394
+ if not skipna :
395
+ return _wrap_results (
396
+ np .apply_along_axis (get_median , axis , values ), dtype )
397
+
398
+ # fastpath for the skipna case
399
+ return _wrap_results (np .nanmedian (values , axis ), dtype )
396
400
397
401
# must return the correct shape, but median is not defined for the
398
402
# empty set so return nans of shape "everything but the passed axis"
0 commit comments