Skip to content

Commit cf94f8c

Browse files
jbrockmendelquintusdias
authored andcommitted
CLN: remove _try_coerce_result altogether (pandas-dev#27683)
1 parent f36c2d7 commit cf94f8c

File tree

3 files changed

+30
-70
lines changed

3 files changed

+30
-70
lines changed

pandas/core/groupby/ops.py

+28-17
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
is_categorical_dtype,
2626
is_complex_dtype,
2727
is_datetime64_any_dtype,
28+
is_datetime64tz_dtype,
2829
is_integer_dtype,
2930
is_numeric_dtype,
3031
is_sparse,
@@ -451,6 +452,7 @@ def wrapper(*args, **kwargs):
451452

452453
def _cython_operation(self, kind, values, how, axis, min_count=-1, **kwargs):
453454
assert kind in ["transform", "aggregate"]
455+
orig_values = values
454456

455457
# can we do this operation with our cython functions
456458
# if not raise NotImplementedError
@@ -475,23 +477,11 @@ def _cython_operation(self, kind, values, how, axis, min_count=-1, **kwargs):
475477
"timedelta64 type does not support {} operations".format(how)
476478
)
477479

478-
arity = self._cython_arity.get(how, 1)
479-
480-
vdim = values.ndim
481-
swapped = False
482-
if vdim == 1:
483-
values = values[:, None]
484-
out_shape = (self.ngroups, arity)
485-
else:
486-
if axis > 0:
487-
swapped = True
488-
assert axis == 1, axis
489-
values = values.T
490-
if arity > 1:
491-
raise NotImplementedError(
492-
"arity of more than 1 is not supported for the 'how' argument"
493-
)
494-
out_shape = (self.ngroups,) + values.shape[1:]
480+
if is_datetime64tz_dtype(values.dtype):
481+
# Cast to naive; we'll cast back at the end of the function
482+
# TODO: possible need to reshape? kludge can be avoided when
483+
# 2D EA is allowed.
484+
values = values.view("M8[ns]")
495485

496486
is_datetimelike = needs_i8_conversion(values.dtype)
497487
is_numeric = is_numeric_dtype(values.dtype)
@@ -513,6 +503,24 @@ def _cython_operation(self, kind, values, how, axis, min_count=-1, **kwargs):
513503
else:
514504
values = values.astype(object)
515505

506+
arity = self._cython_arity.get(how, 1)
507+
508+
vdim = values.ndim
509+
swapped = False
510+
if vdim == 1:
511+
values = values[:, None]
512+
out_shape = (self.ngroups, arity)
513+
else:
514+
if axis > 0:
515+
swapped = True
516+
assert axis == 1, axis
517+
values = values.T
518+
if arity > 1:
519+
raise NotImplementedError(
520+
"arity of more than 1 is not supported for the 'how' argument"
521+
)
522+
out_shape = (self.ngroups,) + values.shape[1:]
523+
516524
try:
517525
func = self._get_cython_function(kind, how, values, is_numeric)
518526
except NotImplementedError:
@@ -581,6 +589,9 @@ def _cython_operation(self, kind, values, how, axis, min_count=-1, **kwargs):
581589
if swapped:
582590
result = result.swapaxes(0, axis)
583591

592+
if is_datetime64tz_dtype(orig_values.dtype):
593+
result = type(orig_values)(result.astype(np.int64), dtype=orig_values.dtype)
594+
584595
return result, names
585596

586597
def aggregate(self, values, how, axis=0, min_count=-1):

pandas/core/internals/blocks.py

+1-52
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,6 @@ def fillna(self, value, limit=None, inplace=False, downcast=None):
417417
if self._can_hold_element(value):
418418
# equivalent: self._try_coerce_args(value) would not raise
419419
blocks = self.putmask(mask, value, inplace=inplace)
420-
blocks = [
421-
b.make_block(values=self._try_coerce_result(b.values)) for b in blocks
422-
]
423420
return self._maybe_downcast(blocks, downcast)
424421

425422
# we can't process the value, but nothing to do
@@ -734,12 +731,7 @@ def _try_coerce_args(self, other):
734731

735732
return other
736733

737-
def _try_coerce_result(self, result):
738-
""" reverse of try_coerce_args """
739-
return result
740-
741734
def _try_coerce_and_cast_result(self, result, dtype=None):
742-
result = self._try_coerce_result(result)
743735
result = self._try_cast_result(result, dtype=dtype)
744736
return result
745737

@@ -1406,7 +1398,7 @@ def func(cond, values, other):
14061398

14071399
try:
14081400
fastres = expressions.where(cond, values, other)
1409-
return self._try_coerce_result(fastres)
1401+
return fastres
14101402
except Exception as detail:
14111403
if errors == "raise":
14121404
raise TypeError(
@@ -1692,7 +1684,6 @@ def putmask(self, mask, new, align=True, inplace=False, axis=0, transpose=False)
16921684
mask = _safe_reshape(mask, new_values.shape)
16931685

16941686
new_values[mask] = new
1695-
new_values = self._try_coerce_result(new_values)
16961687
return [self.make_block(values=new_values)]
16971688

16981689
def _try_cast_result(self, result, dtype=None):
@@ -1870,20 +1861,6 @@ def _slice(self, slicer):
18701861

18711862
return self.values[slicer]
18721863

1873-
def _try_cast_result(self, result, dtype=None):
1874-
"""
1875-
if we have an operation that operates on for example floats
1876-
we want to try to cast back to our EA here if possible
1877-
1878-
result could be a 2-D numpy array, e.g. the result of
1879-
a numeric operation; but it must be shape (1, X) because
1880-
we by-definition operate on the ExtensionBlocks one-by-one
1881-
1882-
result could also be an EA Array itself, in which case it
1883-
is already a 1-D array
1884-
"""
1885-
return result
1886-
18871864
def formatting_values(self):
18881865
# Deprecating the ability to override _formatting_values.
18891866
# Do the warning here, it's only user in pandas, since we
@@ -2443,20 +2420,6 @@ def _try_coerce_args(self, other):
24432420

24442421
return other
24452422

2446-
def _try_coerce_result(self, result):
2447-
""" reverse of try_coerce_args """
2448-
if isinstance(result, np.ndarray):
2449-
if result.ndim == 2:
2450-
# kludge for 2D blocks with 1D EAs
2451-
result = result[0, :]
2452-
if result.dtype == np.float64:
2453-
# needed for post-groupby.median
2454-
result = self._holder._from_sequence(
2455-
result.astype(np.int64), freq=None, dtype=self.values.dtype
2456-
)
2457-
2458-
return result
2459-
24602423
def diff(self, n, axis=0):
24612424
"""1st discrete difference
24622425
@@ -2619,10 +2582,6 @@ def _try_coerce_args(self, other):
26192582

26202583
return other
26212584

2622-
def _try_coerce_result(self, result):
2623-
""" reverse of try_coerce_args / try_operate """
2624-
return result
2625-
26262585
def should_store(self, value):
26272586
return issubclass(
26282587
value.dtype.type, np.timedelta64
@@ -3031,16 +2990,6 @@ def array_dtype(self):
30312990
"""
30322991
return np.object_
30332992

3034-
def _try_coerce_result(self, result):
3035-
""" reverse of try_coerce_args """
3036-
3037-
# GH12564: CategoricalBlock is 1-dim only
3038-
# while returned results could be any dim
3039-
if (not is_categorical_dtype(result)) and isinstance(result, np.ndarray):
3040-
result = _block_shape(result, ndim=self.ndim)
3041-
3042-
return result
3043-
30442993
def to_dense(self):
30452994
# Categorical.get_values returns a DatetimeIndex for datetime
30462995
# categories, so we can't simply use `np.asarray(self.values)` like

pandas/core/internals/managers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ def fast_xs(self, loc):
908908
# Such assignment may incorrectly coerce NaT to None
909909
# result[blk.mgr_locs] = blk._slice((slice(None), loc))
910910
for i, rl in enumerate(blk.mgr_locs):
911-
result[rl] = blk._try_coerce_result(blk.iget((i, loc)))
911+
result[rl] = blk.iget((i, loc))
912912

913913
if is_extension_array_dtype(dtype):
914914
result = dtype.construct_array_type()._from_sequence(result, dtype=dtype)

0 commit comments

Comments
 (0)