Skip to content

Commit

Permalink
rebase codes
Browse files Browse the repository at this point in the history
  • Loading branch information
HH committed Aug 22, 2019
1 parent def01cf commit 85b9eef
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Indexing
- Break reference cycle involving :class:`Index` and other index classes to allow garbage collection of index objects without running the GC. (:issue:`27585`, :issue:`27840`)
- Fix regression in assigning values to a single column of a DataFrame with a ``MultiIndex`` columns (:issue:`27841`).
- Fix regression in ``.ix`` fallback with an ``IntervalIndex`` (:issue:`27865`).
- ``IndexError`` would not raise if missing data in index (:issue:`20519`).

Missing
^^^^^^^
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1279,7 +1279,10 @@ def _get_grouper_for_level(self, mapper, level):
# Remove unobserved levels from level_index
level_index = level_index.take(uniques)

grouper = level_index.take(codes)
if not len(level_index):
grouper = level_index
else:
grouper = level_index.take(codes)

return grouper, codes, level_index

Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2240,6 +2240,30 @@ def test_groupby(self):

tm.assert_dict_equal(result, expected)

def test_groupby_nan_index_value(self):
df = pd.DataFrame([["x", np.nan, 1]], columns=["A", "B", "C"]).set_index(
["A", "B"]
)
result = df.groupby(level=["A", "B"]).C.sum()
result = np.asarray(result)

s = Series([])
s.name = "C"
expected = s.astype("int64")
expected = np.asarray(expected)
tm.assert_numpy_array_equal(result, expected)

df = pd.DataFrame(
[["x", np.nan, 1, 2], [None, "y", 3, 4]], columns=["A", "B", "C", "D"]
).set_index(["A", "B", "C"])
result = df.groupby(level=["A", "B"]).D.sum()
s = Series([])
s.name = "D"
expected = s.astype("int64")
result = np.asarray(result)
expected = np.asarray(expected)
tm.assert_numpy_array_equal(result, expected)

@pytest.mark.parametrize(
"mi,expected",
[
Expand Down

0 comments on commit 85b9eef

Please sign in to comment.