Skip to content

Commit 74e4544

Browse files
committed
ENH ohlc resample for DataFrame
1 parent 6ffed43 commit 74e4544

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ pandas 0.13
5656
- Significant table writing performance improvements in ``HDFStore``
5757
- JSON date serialisation now performed in low-level C code.
5858
- Add ``drop_level`` argument to xs (:issue:`4180`)
59+
- Can now resample a DataFrame with ohlc (:issue:`2320`)
5960
- ``Index.copy()`` and ``MultiIndex.copy()`` now accept keyword arguments to
6061
change attributes (i.e., ``names``, ``levels``, ``labels``)
6162
(:issue:`4039`)

pandas/core/groupby.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,13 @@ def ohlc(self):
430430
431431
For multiple groupings, the result index will be a MultiIndex
432432
"""
433+
if isinstance(self.obj, com.ABCDataFrame):
434+
from pandas.tools.merge import concat
435+
return concat((col_groupby._cython_agg_general('ohlc')
436+
for _, col_groupby in self._iterate_column_groupbys()),
437+
keys=self.obj.columns,
438+
axis=1)
439+
433440
return self._cython_agg_general('ohlc')
434441

435442
def nth(self, n):
@@ -1619,7 +1626,6 @@ def _iterate_slices(self):
16191626
for val in slice_axis:
16201627
if val in self.exclusions:
16211628
continue
1622-
16231629
yield val, slicer(val)
16241630

16251631
def _cython_agg_general(self, how, numeric_only=True):
@@ -2233,6 +2239,12 @@ def _wrap_agged_blocks(self, blocks):
22332239

22342240
return result.convert_objects()
22352241

2242+
def _iterate_column_groupbys(self):
2243+
for i, colname in enumerate(self.obj.columns):
2244+
yield colname, SeriesGroupBy(self.obj.iloc[:, i], selection=colname,
2245+
grouper=self.grouper,
2246+
exclusions=self.exclusions)
2247+
22362248

22372249
from pandas.tools.plotting import boxplot_frame_groupby
22382250
DataFrameGroupBy.boxplot = boxplot_frame_groupby

pandas/tseries/tests/test_resample.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,31 @@ def test_resample_ohlc(self):
259259
self.assertEquals(xs['low'], s[:5].min())
260260
self.assertEquals(xs['close'], s[4])
261261

262+
def test_resample_ohlc_dataframe(self):
263+
df = (pd.DataFrame({'PRICE': {Timestamp('2011-01-06 10:59:05', tz=None): 24990,
264+
Timestamp('2011-01-06 12:43:33', tz=None): 25499,
265+
Timestamp('2011-01-06 12:54:09', tz=None): 25499},
266+
'VOLUME': {Timestamp('2011-01-06 10:59:05', tz=None): 1500000000,
267+
Timestamp('2011-01-06 12:43:33', tz=None): 5000000000,
268+
Timestamp('2011-01-06 12:54:09', tz=None): 100000000}})
269+
).reindex_axis(['VOLUME', 'PRICE'], axis=1)
270+
res = df.resample('H', how='ohlc')
271+
exp = pd.concat([df['VOLUME'].resample('H', how='ohlc'),
272+
df['PRICE'].resample('H', how='ohlc')],
273+
axis=1,
274+
keys=['VOLUME', 'PRICE'])
275+
assert_frame_equal(exp, res)
276+
277+
df.columns = [['a', 'b'], ['c', 'd']]
278+
res = df.resample('H', how='ohlc')
279+
exp.columns = pd.MultiIndex.from_tuples([('a', 'c', 'open'), ('a', 'c', 'high'),
280+
('a', 'c', 'low'), ('a', 'c', 'close'), ('b', 'd', 'open'),
281+
('b', 'd', 'high'), ('b', 'd', 'low'), ('b', 'd', 'close')])
282+
assert_frame_equal(exp, res)
283+
284+
# dupe columns fail atm
285+
# df.columns = ['PRICE', 'PRICE']
286+
262287
def test_resample_reresample(self):
263288
dti = DatetimeIndex(
264289
start=datetime(2005, 1, 1), end=datetime(2005, 1, 10),

0 commit comments

Comments
 (0)