66
77import numpy as np
88from pandas import compat
9- from pandas ._libs import tslib , algos , lib
9+ from pandas ._libs import tslib , lib
1010from pandas .core .dtypes .common import (
1111 _get_dtype ,
1212 is_float , is_scalar ,
@@ -370,14 +370,13 @@ def nanmean(values, axis=None, skipna=True):
370370@bottleneck_switch ()
371371def nanmedian (values , axis = None , skipna = True ):
372372
373- values , mask , dtype , dtype_max = _get_values (values , skipna )
374-
375373 def get_median (x ):
376374 mask = notna (x )
377375 if not skipna and not mask .all ():
378376 return np .nan
379- return algos . median ( com . _values_from_object ( x [mask ]) )
377+ return np . nanmedian ( x [mask ])
380378
379+ values , mask , dtype , dtype_max = _get_values (values , skipna )
381380 if not is_float_dtype (values ):
382381 values = values .astype ('f8' )
383382 values [mask ] = np .nan
@@ -389,10 +388,15 @@ def get_median(x):
389388
390389 # an array from a frame
391390 if values .ndim > 1 :
391+
392392 # there's a non-empty array to apply over otherwise numpy raises
393393 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 )
396400
397401 # must return the correct shape, but median is not defined for the
398402 # empty set so return nans of shape "everything but the passed axis"
0 commit comments