Repository: pandas-dev/pandas

Issue:
Median returns odd value when applied to timedeltas
``` python
from pandas import DataFrame
from numpy import datetime64
data = ['2015-02-03', '2015-02-07']
data = DataFrame(data, dtype=datetime64)
data.diff().median()
```

This code returns 

``` python
0   -53374 days +00:06:21.572612
dtype: timedelta64[ns]
```

while I would expect it to return the same as data.diff().mean()

``` python
0   4 days
dtype: timedelta64[ns]
```

Here is my setup

``` python
INSTALLED VERSIONS
------------------
commit: None
python: 2.7.6.final.0
python-bits: 64
OS: Darwin
OS-release: 14.4.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8

pandas: 0.16.0
nose: 1.3.4
Cython: None
numpy: 1.9.2
scipy: 0.14.0
statsmodels: None
IPython: 2.3.1
sphinx: None
patsy: None
dateutil: 2.4.2
pytz: 2015.2
bottleneck: None
tables: None
numexpr: None
matplotlib: 1.4.2
openpyxl: 2.1.3
xlrd: 0.9.3
xlwt: 0.7.5
xlsxwriter: None
lxml: None
bs4: 4.3.2
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: 2.5.4 (dt dec pq3 ext)
```

Median returns odd value when applied to timedeltas
``` python
from pandas import DataFrame
from numpy import datetime64
data = ['2015-02-03', '2015-02-07']
data = DataFrame(data, dtype=datetime64)
data.diff().median()
```

This code returns 

``` python
0   -53374 days +00:06:21.572612
dtype: timedelta64[ns]
```

while I would expect it to return the same as data.diff().mean()

``` python
0   4 days
dtype: timedelta64[ns]
```

Here is my setup

``` python
INSTALLED VERSIONS
------------------
commit: None
python: 2.7.6.final.0
python-bits: 64
OS: Darwin
OS-release: 14.4.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8

pandas: 0.16.0
nose: 1.3.4
Cython: None
numpy: 1.9.2
scipy: 0.14.0
statsmodels: None
IPython: 2.3.1
sphinx: None
patsy: None
dateutil: 2.4.2
pytz: 2015.2
bottleneck: None
tables: None
numexpr: None
matplotlib: 1.4.2
openpyxl: 2.1.3
xlrd: 0.9.3
xlwt: 0.7.5
xlsxwriter: None
lxml: None
bs4: 4.3.2
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: 2.5.4 (dt dec pq3 ext)
```



Discussion:
xref #9442. The algo that is doing this is converting to `float64` so its losing precision. (as I don't think the underling ops is defined for `int64`).

this needs special handling for `int64` in `core/nanops/nanmedian`. PR's are welcome

xref #9442. The algo that is doing this is converting to `float64` so its losing precision. (as I don't think the underling ops is defined for `int64`).

this needs special handling for `int64` in `core/nanops/nanmedian`. PR's are welcome


Resolving patch:
diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py
--- a/pandas/core/nanops.py
+++ b/pandas/core/nanops.py
@@ -285,6 +285,7 @@ def get_median(x):
 
     if values.dtype != np.float64:
         values = values.astype('f8')
+        values[mask] = np.nan
 
     if axis is None:
         values = values.ravel()


Test patch:
diff --git a/pandas/tseries/tests/test_timedeltas.py b/pandas/tseries/tests/test_timedeltas.py
--- a/pandas/tseries/tests/test_timedeltas.py
+++ b/pandas/tseries/tests/test_timedeltas.py
@@ -614,7 +614,7 @@ def test_timedelta_ops(self):
         self.assertEqual(result, expected)
 
         result = td.median()
-        expected = to_timedelta('00:00:08')
+        expected = to_timedelta('00:00:09')
         self.assertEqual(result, expected)
 
         result = td.to_frame().median()
@@ -641,6 +641,14 @@ def test_timedelta_ops(self):
         for op in ['skew','kurt','sem','var','prod']:
             self.assertRaises(TypeError, lambda : getattr(td,op)())
 
+        # GH 10040
+        # make sure NaT is properly handled by median()
+        s = Series([Timestamp('2015-02-03'), Timestamp('2015-02-07')])
+        self.assertEqual(s.diff().median(), timedelta(days=4))
+
+        s = Series([Timestamp('2015-02-03'), Timestamp('2015-02-07'), Timestamp('2015-02-15')])
+        self.assertEqual(s.diff().median(), timedelta(days=6))
+
     def test_timedelta_ops_scalar(self):
         # GH 6808
         base = pd.to_datetime('20130101 09:01:12.123456')