Skip to content

Commit c21a755

Browse files
committed
Fixes grouping by nan values after update pandas to 1.3.0
1 parent 931419a commit c21a755

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

_unittests/ut_df/test_pandas_groupbynan.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ def test_pandas_groupbynan(self):
3535
self.assertTrue(numpy.isnan(li[-1]))
3636
except AssertionError as e:
3737
raise AssertionError(
38-
"Issue with value {0}\n--df--\n{1}\n--co--\n{2}".format(li, df, co)) from e
38+
"Issue with value {}\n--df--\n{}\n--gr--\n{}\n--co--\n{}".format(
39+
li, df, gr.count(), co)) from e
3940

4041
for ty in types:
4142
data = [{"this": "cst", "type": "tt1=" + str(ty[0]), "value": ty[1]},

pandas_streaming/df/dataframe_helpers.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -419,14 +419,27 @@ def pandas_groupby_nan(df, by, axis=0, as_index=False, suffix=None, nanback=True
419419
res.grouper.groupings[0]._group_index = Index(new_val)
420420
res.grouper.groupings[0].obj[b].replace(
421421
fnan, numpy.nan, inplace=True)
422-
if isinstance(res.grouper.groupings[0].grouper, numpy.ndarray):
423-
arr = numpy.array(new_val)
424-
res.grouper.groupings[0].grouper = arr
425-
if hasattr(res.grouper.groupings[0], '_cache') and 'result_index' in res.grouper.groupings[0]._cache:
426-
del res.grouper.groupings[0]._cache['result_index']
422+
if hasattr(res.grouper, 'grouping'):
423+
if isinstance(res.grouper.groupings[0].grouper, numpy.ndarray):
424+
arr = numpy.array(new_val)
425+
res.grouper.groupings[0].grouper = arr
426+
if (hasattr(res.grouper.groupings[0], '_cache') and
427+
'result_index' in res.grouper.groupings[0]._cache):
428+
del res.grouper.groupings[0]._cache['result_index']
429+
else:
430+
raise NotImplementedError("Not implemented for type: {0}".format(
431+
type(res.grouper.groupings[0].grouper)))
427432
else:
428-
raise NotImplementedError("Not implemented for type: {0}".format(
429-
type(res.grouper.groupings[0].grouper)))
433+
grouper = res.grouper._get_grouper()
434+
if isinstance(grouper, numpy.ndarray):
435+
arr = numpy.array(new_val)
436+
res.grouper.groupings[0].grouping_vector = arr
437+
if (hasattr(res.grouper.groupings[0], '_cache') and
438+
'result_index' in res.grouper.groupings[0]._cache):
439+
res.grouper.groupings[0]._cache = {}
440+
else:
441+
raise NotImplementedError("Not implemented for type: {0}".format(
442+
type(res.grouper.groupings[0].grouper)))
430443
res.grouper._cache['result_index'] = res.grouper.groupings[0]._group_index
431444
else:
432445
if not nanback:

0 commit comments

Comments
 (0)