BUG: window function count should count anything non-null · Issue #12541 · pandas-dev/pandas (original) (raw)

Right now np.Inf and non-numeric types are not counted.

Code Sample, a copy-pastable example if possible

import pandas as pd
import numpy as np
from pandas import DataFrame

df = DataFrame({'A': pd.date_range('20130101',periods=4), 'B': [0, 1, 2, np.Inf], 'C':['a', 'b', 'c', 'd']})

roll = df.rolling(window=2)
res = roll.count()
print(res)
## partial output A, B don't error
     A    B
0  1.0  1.0
1  2.0  2.0
2  2.0  2.0
3  2.0  1.0
## column C errors

Expected Output

   A  B  C
0  1  1  1
1  2  2  2
2  2  2  2
3  2  2  2

output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit: a0aaad98e8c567a3a04aef165cdb159fbd98d13f
python: 3.5.1.final.0
python-bits: 64
OS: Linux
OS-release: 3.13.0-76-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.18.0rc1+77.ga0aaad9.dirty
nose: 1.3.7
pip: 8.0.1
setuptools: 19.4
Cython: 0.23.4
numpy: 1.10.2
scipy: None
statsmodels: None
xarray: None
IPython: 4.0.0
sphinx: None
patsy: None
dateutil: 2.4.2
pytz: 2015.7
blosc: None
bottleneck: 1.0.0
tables: None
numexpr: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.10
pymysql: None
psycopg2: None
jinja2: None
boto: None

I think it can be patched, together with #12538, by

--- a/pandas/core/window.py
+++ b/pandas/core/window.py
@@ -509,19 +509,9 @@ class _Rolling_and_Expanding(_Rolling):
         blocks, obj = self._create_blocks(how=None)
         results = []
         for b in blocks:
-
-            if com.needs_i8_conversion(b.values):
-                result = b.notnull().astype(int)
-            else:
-                try:
-                    result = np.isfinite(b).astype(float)
-                except TypeError:
-                    result = np.isfinite(b.astype(float)).astype(float)
-
-                result[pd.isnull(result)] = 0
-
-            result = self._constructor(result, window=window, min_perio
-                                       center=self.center).sum()
+            result = self._constructor(
+                b.notnull(), window=window, min_periods=0, center=self.
+            ).sum().astype(int)