-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Consolidate nth / last object Groupby Implementations #19610
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2086,47 +2086,45 @@ def test_median_empty_bins(self): | |
expected = df.groupby(bins).agg(lambda x: x.median()) | ||
assert_frame_equal(result, expected) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a tests with the numeric ops on groupby / object and assert they raise There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assuming you want this in a different change no problem. That said, do you consider the numeric ops to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes let's do this in another change. |
||
|
||
def test_groupby_non_arithmetic_agg_types(self): | ||
@pytest.mark.parametrize("dtype", [ | ||
'int8', 'int16', 'int32', 'int64', 'float32', 'float64']) | ||
@pytest.mark.parametrize("method,data", [ | ||
('first', {'df': [{'a': 1, 'b': 1}, {'a': 2, 'b': 3}]}), | ||
('last', {'df': [{'a': 1, 'b': 2}, {'a': 2, 'b': 4}]}), | ||
('min', {'df': [{'a': 1, 'b': 1}, {'a': 2, 'b': 3}]}), | ||
('max', {'df': [{'a': 1, 'b': 2}, {'a': 2, 'b': 4}]}), | ||
('nth', {'df': [{'a': 1, 'b': 2}, {'a': 2, 'b': 4}], | ||
'args': [1]}), | ||
('count', {'df': [{'a': 1, 'b': 2}, {'a': 2, 'b': 2}], | ||
'out_type': 'int64'}) | ||
]) | ||
def test_groupby_non_arithmetic_agg_types(self, dtype, method, data): | ||
# GH9311, GH6620 | ||
df = pd.DataFrame( | ||
[{'a': 1, 'b': 1}, | ||
{'a': 1, 'b': 2}, | ||
{'a': 2, 'b': 3}, | ||
{'a': 2, 'b': 4}]) | ||
|
||
dtypes = ['int8', 'int16', 'int32', 'int64', 'float32', 'float64'] | ||
df['b'] = df.b.astype(dtype) | ||
|
||
grp_exp = {'first': {'df': [{'a': 1, 'b': 1}, {'a': 2, 'b': 3}]}, | ||
'last': {'df': [{'a': 1, 'b': 2}, {'a': 2, 'b': 4}]}, | ||
'min': {'df': [{'a': 1, 'b': 1}, {'a': 2, 'b': 3}]}, | ||
'max': {'df': [{'a': 1, 'b': 2}, {'a': 2, 'b': 4}]}, | ||
'nth': {'df': [{'a': 1, 'b': 2}, {'a': 2, 'b': 4}], | ||
'args': [1]}, | ||
'count': {'df': [{'a': 1, 'b': 2}, {'a': 2, 'b': 2}], | ||
'out_type': 'int64'}} | ||
if 'args' not in data: | ||
data['args'] = [] | ||
|
||
for dtype in dtypes: | ||
df_in = df.copy() | ||
df_in['b'] = df_in.b.astype(dtype) | ||
|
||
for method, data in compat.iteritems(grp_exp): | ||
if 'args' not in data: | ||
data['args'] = [] | ||
|
||
if 'out_type' in data: | ||
out_type = data['out_type'] | ||
else: | ||
out_type = dtype | ||
if 'out_type' in data: | ||
out_type = data['out_type'] | ||
else: | ||
out_type = dtype | ||
|
||
exp = data['df'] | ||
df_out = pd.DataFrame(exp) | ||
exp = data['df'] | ||
df_out = pd.DataFrame(exp) | ||
|
||
df_out['b'] = df_out.b.astype(out_type) | ||
df_out.set_index('a', inplace=True) | ||
df_out['b'] = df_out.b.astype(out_type) | ||
df_out.set_index('a', inplace=True) | ||
|
||
grpd = df_in.groupby('a') | ||
t = getattr(grpd, method)(*data['args']) | ||
assert_frame_equal(t, df_out) | ||
grpd = df.groupby('a') | ||
t = getattr(grpd, method)(*data['args']) | ||
assert_frame_equal(t, df_out) | ||
|
||
def test_groupby_non_arithmetic_agg_intlike_precision(self): | ||
# GH9311, GH6620 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was really hoping to not even have this conditional, but when trying
resx = np.empty_like(out)
and evenresx = np.empty_like(out, dtype='object')
it kept SegFaulting on objects