Skip to content

Commit ec86497

Browse files
committed
BUG: make GroupBy.apply work properly with TimeGrouper. close #2300
1 parent 07318fa commit ec86497

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

RELEASE.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,10 @@ pandas 0.10.0
134134
- Fixes assigning scalars and array to hierarchical column chunk (#1803)
135135
- Fixed a UnicdeDecodeError with series tidy_repr (#2225)
136136
- Fixed issued with duplicate keys in an index (#2347, #2380)
137-
- Fixed issues related to Hash randomization, on by default starting with 3.3 (#2331)
137+
- Fixed issues re: Hash randomization, default on starting w/ py3.3 (#2331)
138138
- Fixed issue with missing attributes after loading a pickled dataframe (#2431)
139139
- Fix Timestamp formatting with tzoffset time zone in dateutil 2.1 (#2443)
140+
- Fix GroupBy.apply issue when using BinGrouper to do ts binning (#2300)
140141

141142
pandas 0.9.1
142143
============

pandas/core/groupby.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1720,7 +1720,7 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
17201720
if isinstance(values[0], DataFrame):
17211721
return self._concat_objects(keys, values,
17221722
not_indexed_same=not_indexed_same)
1723-
else:
1723+
elif hasattr(self.grouper, 'groupings'):
17241724
if len(self.grouper.groupings) > 1:
17251725
key_index = MultiIndex.from_tuples(keys, names=key_names)
17261726
else:
@@ -1757,6 +1757,10 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
17571757
columns=columns)
17581758
else:
17591759
return Series(values, index=key_index)
1760+
else:
1761+
# Handle cases like BinGrouper
1762+
return self._concat_objects(keys, values,
1763+
not_indexed_same=not_indexed_same)
17601764

17611765
def transform(self, func, *args, **kwargs):
17621766
"""

pandas/tseries/tests/test_resample.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,22 @@ def test_numpy_reduction(self):
963963

964964
def test_apply_iteration(self):
965965
# #2300
966-
pass
966+
N = 1000
967+
ind = pd.date_range(start="2000-01-01", freq="D", periods=N)
968+
df = DataFrame({'open':1, 'close':2}, index=ind)
969+
tg = TimeGrouper('M')
970+
971+
grouper = tg.get_grouper(df)
972+
973+
# Errors
974+
975+
grouped = df.groupby(grouper, group_keys=False)
976+
f = lambda df: df['close'] / df['open']
977+
978+
# it works!
979+
result = grouped.apply(f)
980+
self.assertTrue(result.index.equals(df.index))
981+
967982

968983
if __name__ == '__main__':
969984
nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],

0 commit comments

Comments
 (0)