Skip to content

Commit 94d02bc

Browse files
Giacomo FerroniAnkurDedania
Giacomo Ferroni
authored andcommitted
BUG: Fix bug in window function count should count anything non-null
closes pandas-dev#12541 Author: Giacomo Ferroni <[email protected]> Author: Giacomo <[email protected]> Closes pandas-dev#15196 from mralgos/gh12541 and squashes the following commits: 65d70eb [Giacomo Ferroni] Added Periods to the test 94084b4 [Giacomo] Merge branch 'master' into gh12541 9621315 [Giacomo Ferroni] Added extra test and updated whatsnew cb84935 [Giacomo Ferroni] pylint checks 26c86a5 [Giacomo Ferroni] Test added for GH12541 ea49e77 [Giacomo Ferroni] Fix for GH12541
1 parent 983fdd2 commit 94d02bc

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

doc/source/whatsnew/v0.20.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ Bug Fixes
432432
- Bug in ``pd.pivot_table()`` where no error was raised when values argument was not in the columns (:issue:`14938`)
433433

434434
- Bug in ``.to_json()`` where ``lines=True`` and contents (keys or values) contain escaped characters (:issue:`15096`)
435+
- Bug in ``.rolling/expanding()`` functions where ``count()`` was not counting ``np.Inf``, nor handling ``object`` dtypes (:issue:`12541`)
435436

436437
- Bug in ``DataFrame.groupby().describe()`` when grouping on ``Index`` containing tuples (:issue:`14848`)
437438
- Bug in creating a ``MultiIndex`` with tuples and not passing a list of names; this will now raise ``ValueError`` (:issue:`15110`)

pandas/core/window.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -762,17 +762,7 @@ def count(self):
762762

763763
results = []
764764
for b in blocks:
765-
766-
if needs_i8_conversion(b.values):
767-
result = b.notnull().astype(int)
768-
else:
769-
try:
770-
result = np.isfinite(b).astype(float)
771-
except TypeError:
772-
result = np.isfinite(b.astype(float)).astype(float)
773-
774-
result[pd.isnull(result)] = 0
775-
765+
result = b.notnull().astype(int)
776766
result = self._constructor(result, window=window, min_periods=0,
777767
center=self.center).sum()
778768
results.append(result)

pandas/tests/test_window.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,49 @@ def f():
207207
'A', 'ra', 'std'), ('B', 'rb', 'mean'), ('B', 'rb', 'std')])
208208
tm.assert_frame_equal(result, expected, check_like=True)
209209

210+
def test_count_nonnumeric_types(self):
211+
# GH12541
212+
cols = ['int', 'float', 'string', 'datetime', 'timedelta', 'periods',
213+
'fl_inf', 'fl_nan', 'str_nan', 'dt_nat', 'periods_nat']
214+
215+
df = DataFrame(
216+
{'int': [1, 2, 3],
217+
'float': [4., 5., 6.],
218+
'string': list('abc'),
219+
'datetime': pd.date_range('20170101', periods=3),
220+
'timedelta': pd.timedelta_range('1 s', periods=3, freq='s'),
221+
'periods': [pd.Period('2012-01'), pd.Period('2012-02'),
222+
pd.Period('2012-03')],
223+
'fl_inf': [1., 2., np.Inf],
224+
'fl_nan': [1., 2., np.NaN],
225+
'str_nan': ['aa', 'bb', np.NaN],
226+
'dt_nat': [pd.Timestamp('20170101'), pd.Timestamp('20170203'),
227+
pd.Timestamp(None)],
228+
'periods_nat': [pd.Period('2012-01'), pd.Period('2012-02'),
229+
pd.Period(None)]},
230+
columns=cols)
231+
232+
expected = DataFrame(
233+
{'int': [1., 2., 2.],
234+
'float': [1., 2., 2.],
235+
'string': [1., 2., 2.],
236+
'datetime': [1., 2., 2.],
237+
'timedelta': [1., 2., 2.],
238+
'periods': [1., 2., 2.],
239+
'fl_inf': [1., 2., 2.],
240+
'fl_nan': [1., 2., 1.],
241+
'str_nan': [1., 2., 1.],
242+
'dt_nat': [1., 2., 1.],
243+
'periods_nat': [1., 2., 1.]},
244+
columns=cols)
245+
246+
result = df.rolling(window=2).count()
247+
tm.assert_frame_equal(result, expected)
248+
249+
result = df.rolling(1).count()
250+
expected = df.notnull().astype(float)
251+
tm.assert_frame_equal(result, expected)
252+
210253
def test_window_with_args(self):
211254
tm._skip_if_no_scipy()
212255

0 commit comments

Comments
 (0)