Repository: pandas-dev/pandas

Issue:
Failing test on win/py2.6/64, TestResample:test_how_lambda_functions
This has failed intermittently a couple of times in the last week or two.

```
=============================================================
=======================
FAILURE: test_how_lambda_functions (pandas.tseries.tests.test_resample.TestResample)
------------------------------------------------------------------------------------
Traceback (most recent call last):
  File "c:\Python27-AMD64\Lib\unittest\case.py", line 331, in run
    testMethod()
  File "C:\workspace\pandas_tests\BITS\64\PYTHONVER\27\pandas\tseries\tests\test_resample.py", line 640, in test_how_lambda_functions
    tm.assert_series_equal(result['bar'], bar_exp)
  File "C:\workspace\pandas_tests\BITS\64\PYTHONVER\27\pandas\util\testing.py", line 448, in assert_series_equal
    assert_almost_equal(left.values, right.values, check_less_precise)
  File "testing.pyx", line 58, in pandas._testing.assert_almost_equal (pandas\src\testing.c:2561)
  File "testing.pyx", line 93, in pandas._testing.assert_almost_equal (pandas\src\testing.c:1803)
  File "testing.pyx", line 107, in pandas._testing.assert_almost_equal (pandas\src\testing.c:2023)
AssertionError: First object is not null, second is null: inf != nan
```



Discussion:
I'll have. look tomorrow 

@jreback , I've been chasing this down, I think it's an actual bug. 
So far i've only seen it on windows, on py2.6.6/64 bit, with numpy 1.8.0.
I was able to reproduce and capture a pickle of the data that causes the problem.
I don't have python2.6 on this linux box, but the problem doesn't appear
on 2.7 with the same numpy.

I can't inline the float64 data because the repr roundtrip changes the data,
presumably roundoff. I'll mail you the pickle.

Reproduce:

``` python
import pandas as pd
from pandas import *
ts = pd.read_pickle('GH6136.pickle')
# on win/py2.6.6 np1.8.0 vs linux/py2.7.5 np1.8.0
print ts.resample('M', how={'bar': lambda x: x.std(ddof=1)})
# similarly, on linux above and below produce the same result
# on window/... they are different see below. That's the cause
# of the failing test
ts.resample('M', how='std')
```

```
INSTALLED VERSIONS
------------------
commit: 1775ba10518d7026bde95d9d77a1aab52f025033
python: 2.6.6.final.0
python-bits: 64
OS: Windows
OS-release: 7
Cython: 0.19.2

numpy: 1.8.0
bottleneck: None
numexpr: None
```

gives

``` python

In [71]: ts.resample('M', how={'bar': lambda x: x.std(ddof=1)})
Out[71]:
                 bar
2000-01-31  0.896238
2000-02-29  0.944715
2000-03-31  1.149277
2000-04-30    1.#INF

[4 rows x 1 columns]

In [72]: ts.resample('M', how='std')
Out[72]:
2000-01-31    0.896238
2000-02-29    0.944715
2000-03-31    1.149277
2000-04-30         NaN
Freq: M, dtype: float64

In [73]: np.version.git_revision
Out[73]: 'a60b3901cd635d28bef8328e83bafd35ce631e08'
```

while

```
INSTALLED VERSIONS
------------------
commit: b20fc1540c51ad4d69ac7b71c8d53ee696c52a57
python: 2.7.5.final.0
python-bits: 64
OS: Linux

numpy: 1.8.0
bottleneck: None
numexpr: None
```

gives

``` python
In [24]: ts.resample('M', how={'bar': lambda x: x.std(ddof=1)})
Out[24]: 
                 bar
2000-01-31  0.896238
2000-02-29  0.944715
2000-03-31  1.149277
2000-04-30       NaN

[4 rows x 1 columns]
In [25]: 
In [
In [25]: ts.resample('M', how='std')
Out[25]: 
2000-01-31    0.896238
2000-02-29    0.944715
2000-03-31    1.149277
2000-04-30         NaN
Freq: M, dtype: float64

>>> np.version.git_revision
'a60b3901cd635d28bef8328e83bafd35ce631e08'
```

moved to 0.13.1, push to 0.14 if we must.

Damn, we're doing that mindread thing again.

detailed!

I have. py2.6 64 windows so I can try to reproduce 


Resolving patch:
diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py
--- a/pandas/core/nanops.py
+++ b/pandas/core/nanops.py
@@ -302,14 +302,25 @@ def nanvar(values, axis=None, skipna=True, ddof=1):
     else:
         count = float(values.size - mask.sum())
 
+    d = count-ddof
     if skipna:
         values = values.copy()
         np.putmask(values, mask, 0)
 
+    # always return NaN, never inf
+    if np.isscalar(count):
+        if count <= ddof:
+            count = np.nan
+            d = np.nan
+    else:
+        mask = count <= ddof
+        if mask.any():
+            np.putmask(d, mask, np.nan)
+            np.putmask(count, mask, np.nan)
+
     X = _ensure_numeric(values.sum(axis))
     XX = _ensure_numeric((values ** 2).sum(axis))
-    return np.fabs((XX - X ** 2 / count) / (count - ddof))
-
+    return np.fabs((XX - X ** 2 / count) / d)
 
 @bottleneck_switch()
 def nanmin(values, axis=None, skipna=True):


Test patch:
diff --git a/pandas/tests/test_series.py b/pandas/tests/test_series.py
--- a/pandas/tests/test_series.py
+++ b/pandas/tests/test_series.py
@@ -1832,6 +1832,14 @@ def test_var_std(self):
         expected = np.var(self.ts.values, ddof=4)
         assert_almost_equal(result, expected)
 
+        # 1 - element series with ddof=1
+        s = self.ts.iloc[[0]]
+        result = s.var(ddof=1)
+        self.assert_(isnull(result))
+
+        result = s.std(ddof=1)
+        self.assert_(isnull(result))
+
     def test_skew(self):
         _skip_if_no_scipy()