Skip to content

Commit 4bd486e

Browse files
committed
review comments
1 parent 86090bf commit 4bd486e

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

doc/source/whatsnew/v0.25.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ of ``object`` dtype. :attr:`Series.str` will now infer the dtype data *within* t
323323
ufuncs on Extension Dtype
324324
^^^^^^^^^^^^^^^^^^^^^^^^^
325325

326-
Operations with ``numpy`` ufuncs on Extension Arrays, including Sparse Dtypes will now preserve the
326+
Operations with ``numpy`` ufuncs on DataFrames with Extension Arrays, including Sparse Dtypes will now preserve the
327327
resulting dtypes to same as the input dtype; previously this would coerce to a dense dtype. (:issue:`23743`)
328328

329329
.. ipython:: python

pandas/core/groupby/groupby.py

+19-16
Original file line numberDiff line numberDiff line change
@@ -1299,7 +1299,7 @@ def _add_numeric_operations(cls):
12991299
"""
13001300

13011301
def groupby_function(name, alias, npfunc,
1302-
numeric_only=True, _convert=False,
1302+
numeric_only=True,
13031303
min_count=-1):
13041304

13051305
_local_template = """
@@ -1321,27 +1321,30 @@ def f(self, **kwargs):
13211321
kwargs['min_count'] = min_count
13221322

13231323
self._set_group_selection()
1324+
1325+
# try a cython aggregation if we can
13241326
try:
13251327
return self._cython_agg_general(
13261328
alias, alt=npfunc, **kwargs)
13271329
except AssertionError as e:
13281330
raise SpecificationError(str(e))
13291331
except Exception:
1330-
result = self.aggregate(
1331-
lambda x: npfunc(x, axis=self.axis))
1332-
1333-
# coerce the columns if we can
1334-
if isinstance(result, DataFrame):
1335-
for col in result.columns:
1336-
result[col] = self._try_cast(
1337-
result[col], self.obj[col])
1338-
else:
1339-
result = self._try_cast(
1340-
result, self.obj)
1341-
1342-
if _convert:
1343-
result = result._convert(datetime=True)
1344-
return result
1332+
pass
1333+
1334+
# apply a non-cython aggregation
1335+
result = self.aggregate(
1336+
lambda x: npfunc(x, axis=self.axis))
1337+
1338+
# coerce the resulting columns if we can
1339+
if isinstance(result, DataFrame):
1340+
for col in result.columns:
1341+
result[col] = self._try_cast(
1342+
result[col], self.obj[col])
1343+
else:
1344+
result = self._try_cast(
1345+
result, self.obj)
1346+
1347+
return result
13451348

13461349
set_function_name(f, name, cls)
13471350

pandas/core/internals/blocks.py

+8
Original file line numberDiff line numberDiff line change
@@ -1772,8 +1772,16 @@ def _try_cast_result(self, result, dtype=None):
17721772
"""
17731773
if we have an operation that operates on for example floats
17741774
we want to try to cast back to our EA here if possible
1775+
1776+
result could be a 2-D numpy array, e.g. the result of
1777+
a numeric operation; but it must be shape (1, X) because
1778+
we by-definition operate on the ExtensionBlocks one-by-one
1779+
1780+
result could also be an EA Array itself, in which case it
1781+
is already a 1-D array
17751782
"""
17761783
try:
1784+
17771785
result = self._holder._from_sequence(
17781786
np.asarray(result).ravel(), dtype=dtype)
17791787
except Exception:

pandas/tests/groupby/test_function.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,7 @@ def scipy_sem(*args, **kwargs):
479479
('last', lambda x: x.iloc[-1]),
480480
('count', np.size),
481481
pytest.param(
482-
'sem', scipy_sem, marks=[pytest.mark.skipif(
483-
td._skip_if_no_scipy(), reason='scipy not installed')])])
482+
'sem', scipy_sem, marks=td.skip_if_no_scipy)])
484483
def test_ops_general(op, targop):
485484
df = DataFrame(np.random.randn(1000))
486485
labels = np.random.randint(0, 50, size=1000).astype(float)

0 commit comments

Comments
 (0)