Skip to content
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

BUG: transform with nunique should have dtype int64 #35152

Merged
merged 3 commits into from
Jul 10, 2020

Conversation

rhshadrach
Copy link
Member

@rhshadrach rhshadrach commented Jul 6, 2020

cc @WillAyd

Removes casting on transformations which go through _transform_fast. The result is a reduction that is broadcast to the original index, so casting isn't necessary once special care is taken for categoricals when observed=False

If this PR is accepted, I'll close #35130 which is for the same issue.

@rhshadrach rhshadrach added Bug Dtype Conversions Unexpected or buggy dtype conversions Groupby Datetime Datetime data dtype labels Jul 6, 2020
Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

not averse to this cleanup, except for the 'temporarily' setting of obvserved. why do you think we need to do this?

@@ -485,8 +485,13 @@ def transform(self, func, *args, engine="cython", engine_kwargs=None, **kwargs):
# If func is a reduction, we need to broadcast the
# result to the whole group. Compute func result
# and deal with possible broadcasting below.
# Temporarily set observed for dealing with
# categoricals so we don't have to convert dtypes.
observed = self.observed
Copy link
Contributor

Choose a reason for hiding this comment

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

woa? can we not simply handle this at a lower level?

Copy link
Member Author

Choose a reason for hiding this comment

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

  • Computing with observed=True is much more performant than observed=False when the category combinations are not saturated, and slightly less performant when they are. So it seems best to me to run with observed=True since transform will disregard any unobserved combinations.
  • Computing with observed=True makes it so that we don't have to do a type conversion float to int because of np.nan for missing combinations.
  • The next step is to call getattr(self, func)(*args, **kwargs). So I don't see how to handle this at a lower level.

That said, the code as-is is definitely not good - self.observed should be modified using a context manager in case the reduction fails. Something like:

with temp_setattr(self, observed=True) as obj:
    getattr(obj, func)(*args, **kwargs)

where temp_setattr is a context manager like the one found here. Not sure if this is a more palatable change.

Copy link
Contributor

Choose a reason for hiding this comment

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

what i mean is, why don't you just pass observed=True directly in the function?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think you're suggesting doing something like getattr(self, func)(*args, **kwargs, observed=True). Assuming args and kwargs are empty, that is akin to df.groupby(keys).sum(observed=True); but sum doesn't take observed as an argument, only groupby does.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok, can you make a context manager for this, like _group_selection_context conceptually.

@@ -485,8 +485,13 @@ def transform(self, func, *args, engine="cython", engine_kwargs=None, **kwargs):
# If func is a reduction, we need to broadcast the
# result to the whole group. Compute func result
# and deal with possible broadcasting below.
# Temporarily set observed for dealing with
# categoricals so we don't have to convert dtypes.
observed = self.observed
Copy link
Contributor

Choose a reason for hiding this comment

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

what i mean is, why don't you just pass observed=True directly in the function?

@jreback jreback added this to the 1.1 milestone Jul 10, 2020
@jreback
Copy link
Contributor

jreback commented Jul 10, 2020

lgtm ping on green.

@rhshadrach
Copy link
Member Author

@jreback green

@jreback jreback merged commit 8fd4cd3 into pandas-dev:master Jul 10, 2020
@jreback
Copy link
Contributor

jreback commented Jul 10, 2020

thanks @rhshadrach

@rhshadrach rhshadrach deleted the outcast branch July 10, 2020 22:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype Dtype Conversions Unexpected or buggy dtype conversions Groupby
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Groupby.transform on datetime with nunique giving incorrect result
3 participants