|
3 | 3 | import numpy as np
|
4 | 4 | import pytest
|
5 | 5 |
|
6 |
| -from pandas import DataFrame, MultiIndex, Period, Series, Timedelta, Timestamp |
| 6 | +from pandas import (DataFrame, Index, MultiIndex, Period, Series, Timedelta, |
| 7 | + Timestamp) |
7 | 8 | import pandas._testing as tm
|
8 | 9 |
|
9 | 10 |
|
@@ -220,3 +221,55 @@ def test_count_with_only_nans_in_first_group(self):
|
220 | 221 | mi = MultiIndex(levels=[[], ["a", "b"]], codes=[[], []], names=["A", "B"])
|
221 | 222 | expected = Series([], index=mi, dtype=np.int64, name="C")
|
222 | 223 | tm.assert_series_equal(result, expected, check_index_type=False)
|
| 224 | + |
| 225 | + #def test_count_groupby_index_with_nan_in_data(self): |
| 226 | + # # https://github.com/pandas-dev/pandas/issues/21824 |
| 227 | + # df = DataFrame({"Person": ["John", "Myla", "John", "John", "Myla"], |
| 228 | + # "Age": [24., np.nan, 21., 33, 26], |
| 229 | + # "Single": [False, True, True, True, False]}) |
| 230 | + |
| 231 | + # res = df.set_index(["Person", "Single"]).count(level="Person") |
| 232 | + # expected = DataFrame(index=Index(["John", "Myla"], name="Person"), |
| 233 | + # data={"Age": [2, 1]}) |
| 234 | + |
| 235 | + def test_count_groupby_index_with_nan_in_index(self): |
| 236 | + # https://github.com/pandas-dev/pandas/issues/21824 |
| 237 | + df = DataFrame({"Person": ["John", "Myla", None, "John", "Myla"], |
| 238 | + "Age": [24., 5, 21., 33, 26], |
| 239 | + "Single": [False, True, True, True, False]}) |
| 240 | + |
| 241 | + res = df.set_index(["Person", "Single"]).count(level="Person") |
| 242 | + expected = DataFrame(index=Index(["John", "Myla"], name="Person"), |
| 243 | + data={"Age": [2, 2]}) |
| 244 | + |
| 245 | + #def test_count_groupby_index_with_nan_in_data_and_in_index(self): |
| 246 | + # # https://github.com/pandas-dev/pandas/issues/21824 |
| 247 | + # df = DataFrame({"Person": ["John", "Myla", None, "John", "Myla"], |
| 248 | + # "Age": [24., np.nan, 21., 33, 26], |
| 249 | + # "Single": [False, True, True, True, False]}) |
| 250 | + |
| 251 | + # res = df.set_index(["Person", "Single"]).count(level="Person") |
| 252 | + # expected = DataFrame(index=Index(["John", "Myla"], name="Person"), |
| 253 | + # data={"Age": [2, 1]}) |
| 254 | + |
| 255 | + |
| 256 | + def test_count_groupby_column_with_nan_in_groupby_column(self): |
| 257 | + df = DataFrame({"A": [1, 1, 1, 1, 1], "B": [5, 4, np.NaN, 3, 0]}) |
| 258 | + res = df.groupby(["B"]).count() |
| 259 | + expected = DataFrame(index=Index([0.0, 3.0, 4.0, 5.0], name="B"), |
| 260 | + data={"A": [1, 1, 1, 1]}) |
| 261 | + tm.assert_frame_equal(expected, res) |
| 262 | + |
| 263 | + #def test_count_groupby_column_with_nan_in_data(self): |
| 264 | + # df = DataFrame({"A": [1, np.NaN, 1, 1, 1], "B": [5, 4., 2, 3, 0]}) |
| 265 | + # res = df.groupby(["B"]).count() |
| 266 | + # expected = DataFrame(index=Index([0.0, 2.0, 3.0, 4.0, 5.0], name="B"), |
| 267 | + # data={"A": [1, 1, 1, 0, 1]}) |
| 268 | + # tm.assert_frame_equal(expected, res) |
| 269 | + |
| 270 | + #def test_count_groupby_column_with_nan_in_data_and_in_groupby_column(self): |
| 271 | + # df = DataFrame({"A": [1, np.NaN, 1, 1, 1], "B": [5, 4, np.NaN, 3, 0]}) |
| 272 | + # res = df.groupby(["B"]).count() |
| 273 | + # expected = DataFrame(index=Index([0.0, 3.0, 4.0, 5.0], name="B"), |
| 274 | + # data={"A": [1, 1, 0, 1]}) |
| 275 | + # tm.assert_frame_equal(expected, res) |
0 commit comments