Skip to content

Fix bug in window function count should count anything non-null #15196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions pandas/core/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,17 +762,7 @@ def count(self):

results = []
for b in blocks:

if 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 = b.notnull().astype(int)
result = self._constructor(result, window=window, min_periods=0,
center=self.center).sum()
results.append(result)
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,26 @@ def f():
'A', 'ra', 'std'), ('B', 'rb', 'mean'), ('B', 'rb', 'std')])
tm.assert_frame_equal(result, expected, check_like=True)

def test_count_nonnumeric_types(self):
# GH12541
df_inf = DataFrame({'x': [1, 2, 3], 'y': [1., 2., np.Inf]})
df_date = DataFrame({'x': [1, 2, 3],
'y': pd.date_range('20130101',periods=3)})
Copy link
Contributor

@jreback jreback Jan 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add:

  • some NaT in here for the datetimes
  • add in a timedelta example,
  • float example with np.nan.
  • some strings (with np.nan)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you do all of these with a single df (with multiple columns), and a single comparision tests (with the results)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/pandas-dev/pandas/pull/15054/files#diff-d1a05a7cf744e0feb0fc6cd7128903a8 is an example of how to create these various types (though need to add some nans)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NaN and NaT won't be counted. Is that right?

df_inf_date = DataFrame({'x': [1, 2, 3], 'y': [1., 2., np.Inf],
'z': pd.date_range('20170101',periods=3)})

expected_1 = DataFrame([[1,1],[2,2],[2,2]],
columns=['x','y'], dtype=float)
expected_2 = DataFrame([[1,1,1],[2,2,2],[2,2,2]],
columns=['x','y','z'], dtype=float)

self.assert_frame_equal(df_inf.rolling(window=2).count(),
expected_1)
self.assert_frame_equal(df_date.rolling(window=2).count(),
expected_1)
self.assert_frame_equal(df_inf_date.rolling(window=2).count(),
expected_2)

def test_window_with_args(self):
tm._skip_if_no_scipy()

Expand Down