|
37 | 37 | is_datetime_or_timedelta_dtype, is_bool,
|
38 | 38 | is_bool_dtype, AbstractMethodError,
|
39 | 39 | _maybe_fill)
|
40 |
| -from pandas.core.config import option_context |
| 40 | +from pandas.core.config import option_context, is_callable |
41 | 41 | import pandas.lib as lib
|
42 | 42 | from pandas.lib import Timestamp
|
43 | 43 | import pandas.tslib as tslib
|
@@ -643,9 +643,20 @@ def apply(self, func, *args, **kwargs):
|
643 | 643 |
|
644 | 644 | func = self._is_builtin_func(func)
|
645 | 645 |
|
646 |
| - @wraps(func) |
647 |
| - def f(g): |
648 |
| - return func(g, *args, **kwargs) |
| 646 | + # this is needed so we don't try and wrap strings. If we could |
| 647 | + # resolve functions to their callable functions prior, this |
| 648 | + # wouldn't be needed |
| 649 | + if args or kwargs: |
| 650 | + if is_callable(func): |
| 651 | + |
| 652 | + @wraps(func) |
| 653 | + def f(g): |
| 654 | + return func(g, *args, **kwargs) |
| 655 | + else: |
| 656 | + raise ValueError('func must be a callable if args or ' |
| 657 | + 'kwargs are supplied') |
| 658 | + else: |
| 659 | + f = func |
649 | 660 |
|
650 | 661 | # ignore SettingWithCopy here in case the user mutates
|
651 | 662 | with option_context('mode.chained_assignment', None):
|
@@ -2675,7 +2686,7 @@ def _wrap_transformed_output(self, output, names=None):
|
2675 | 2686 | def _wrap_applied_output(self, keys, values, not_indexed_same=False):
|
2676 | 2687 | if len(keys) == 0:
|
2677 | 2688 | # GH #6265
|
2678 |
| - return Series([], name=self.name) |
| 2689 | + return Series([], name=self.name, index=keys) |
2679 | 2690 |
|
2680 | 2691 | def _get_index():
|
2681 | 2692 | if self.grouper.nkeys > 1:
|
@@ -3222,8 +3233,7 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
|
3222 | 3233 | from pandas.core.index import _all_indexes_same
|
3223 | 3234 |
|
3224 | 3235 | if len(keys) == 0:
|
3225 |
| - # XXX |
3226 |
| - return DataFrame({}) |
| 3236 | + return DataFrame(index=keys) |
3227 | 3237 |
|
3228 | 3238 | key_names = self.grouper.names
|
3229 | 3239 |
|
@@ -3646,17 +3656,12 @@ def _gotitem(self, key, ndim, subset=None):
|
3646 | 3656 | def _wrap_generic_output(self, result, obj):
|
3647 | 3657 | result_index = self.grouper.levels[0]
|
3648 | 3658 |
|
3649 |
| - if result: |
3650 |
| - if self.axis == 0: |
3651 |
| - result = DataFrame(result, index=obj.columns, |
3652 |
| - columns=result_index).T |
3653 |
| - else: |
3654 |
| - result = DataFrame(result, index=obj.index, |
3655 |
| - columns=result_index) |
| 3659 | + if self.axis == 0: |
| 3660 | + return DataFrame(result, index=obj.columns, |
| 3661 | + columns=result_index).T |
3656 | 3662 | else:
|
3657 |
| - result = DataFrame(result) |
3658 |
| - |
3659 |
| - return result |
| 3663 | + return DataFrame(result, index=obj.index, |
| 3664 | + columns=result_index) |
3660 | 3665 |
|
3661 | 3666 | def _get_data_to_aggregate(self):
|
3662 | 3667 | obj = self._obj_with_exclusions
|
|
0 commit comments