Skip to content

Commit 6a3cc67

Browse files
weikhorweikhor
authored andcommitted
add
1 parent cf99c9e commit 6a3cc67

File tree

3 files changed

+9
-12
lines changed

3 files changed

+9
-12
lines changed

pandas/core/groupby/generic.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,9 @@ def _wrap_applied_output(
386386
)
387387
assert values is not None
388388

389+
if len(data) == 0 and len(values) != 0:
390+
return values[0].to_frame().T.iloc[:0]
391+
389392
if isinstance(values[0], dict):
390393
# GH #823 #24880
391394
index = self.grouper.result_index
@@ -1005,6 +1008,9 @@ def _wrap_applied_output(
10051008
result = result.astype(data.dtypes, copy=False)
10061009
return result
10071010

1011+
if len(data) == 0 and len(values) != 0:
1012+
return values[0].unstack().to_frame().T.iloc[:0]
1013+
10081014
# GH12824
10091015
first_not_none = next(com.not_none(*values), None)
10101016

pandas/core/groupby/groupby.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2491,24 +2491,15 @@ def ohlc(self) -> DataFrame:
24912491
@doc(DataFrame.describe)
24922492
def describe(self, **kwargs):
24932493
with self._group_selection_context():
2494-
if len(self._selected_obj) == 0:
2495-
if self._selected_obj.ndim == 1:
2496-
return self._selected_obj.describe(**kwargs).to_frame().T.iloc[:0]
2497-
else:
2498-
return (
2499-
self._selected_obj.describe(**kwargs)
2500-
.unstack()
2501-
.to_frame()
2502-
.T.iloc[:0]
2503-
)
2504-
25052494
result = self._python_apply_general(
25062495
lambda x: x.describe(**kwargs),
25072496
self._selected_obj,
25082497
not_indexed_same=True,
25092498
)
25102499
if self.axis == 1:
25112500
return result.T
2501+
elif len(result) == 0:
2502+
return result
25122503
return result.unstack()
25132504

25142505
@final

pandas/core/groupby/ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ def apply(
792792
# If group_keys is empty, then no function calls have been made,
793793
# so we will not have raised even if this is an invalid dtype.
794794
# So do one dummy call here to raise appropriate TypeError.
795-
f(data.iloc[:0])
795+
result_values.append(f(data.iloc[:0]))
796796

797797
return result_values, mutated
798798

0 commit comments

Comments
 (0)