-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Groupby.agg with reduction function with tz aware data #25308
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 7 commits
46943e0
724a69e
54a5b7c
0d1eb55
b4913dc
2aa3a2c
18198f4
c970848
da26b7b
1295d4f
60adcf0
e9ac6e8
619f071
206303a
4ac569c
1339a45
0c0b43a
5bf07a9
95a48d6
7feb4a7
fd1a2ec
3cfe961
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 |
---|---|---|
|
@@ -26,7 +26,9 @@ class providing the base-class of operations. | |
|
||
from pandas.core.dtypes.cast import maybe_downcast_to_dtype | ||
from pandas.core.dtypes.common import ( | ||
ensure_float, is_extension_array_dtype, is_numeric_dtype, is_scalar) | ||
ensure_float, is_datetime64tz_dtype, is_extension_array_dtype, | ||
is_numeric_dtype, is_scalar) | ||
from pandas.core.dtypes.dtypes import DatetimeTZDtype | ||
from pandas.core.dtypes.missing import isna, notna | ||
|
||
import pandas.core.algorithms as algorithms | ||
|
@@ -768,7 +770,17 @@ def _try_cast(self, result, obj, numeric_only=False): | |
# The function can return something of any type, so check | ||
# if the type is compatible with the calling EA. | ||
try: | ||
tz_dtype = None | ||
if is_datetime64tz_dtype(dtype): | ||
# GH 23683 | ||
# Prior results were generated in UTC. Ensure | ||
# We localize to UTC first before converting to | ||
# the target timezone | ||
tz_dtype = dtype | ||
dtype = DatetimeTZDtype(tz='UTC') | ||
result = obj._values._from_sequence(result, dtype=dtype) | ||
if tz_dtype is not None: | ||
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. Looks generally good. Is this something you see us needing to do elsewhere besides just this block in groupby (no idea myself, just asking) 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. It appears that This generally needs to be done when datetimetz data goes through cython routines. |
||
result = result.astype(tz_dtype) | ||
except Exception: | ||
# https://github.com/pandas-dev/pandas/issues/22850 | ||
# pandas has no control over what 3rd-party ExtensionArrays | ||
|
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.
for now let's not nest this under is_extension_array_dtype as its not pretty as written; make it a separate case.
However, am thinking we we need to have a pair of (optional) routines on EA that handle this type of thing, we are tending to do this in more and more places (IIRC recently in msgpack). IOW I think we want something like
to work generically. Note when I say serialize this doesn't mean bytes on the wire (necessarily), rather going thru a transform (e.g. here we shift to UTC then transform, then need to transform back).
If you can create an issue for discussion about this.
cc @jbrockmendel @TomAugspurger @jorisvandenbossche