Skip to content

Commit 859aada

Browse files
committed
Merge pull request #4740 from hayd/ohlc_dataframe
ENH ohlc resample for DataFrame
2 parents 53eec08 + e85ef7d commit 859aada

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Improvements to existing features
7979
- Significant table writing performance improvements in ``HDFStore``
8080
- JSON date serialisation now performed in low-level C code.
8181
- Add ``drop_level`` argument to xs (:issue:`4180`)
82+
- Can now resample a DataFrame with ohlc (:issue:`2320`)
8283
- ``Index.copy()`` and ``MultiIndex.copy()`` now accept keyword arguments to
8384
change attributes (i.e., ``names``, ``levels``, ``labels``)
8485
(:issue:`4039`)

pandas/core/groupby.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1616,7 +1616,6 @@ def _iterate_slices(self):
16161616
for val in slice_axis:
16171617
if val in self.exclusions:
16181618
continue
1619-
16201619
yield val, slicer(val)
16211620

16221621
def _cython_agg_general(self, how, numeric_only=True):
@@ -2230,6 +2229,26 @@ def _wrap_agged_blocks(self, blocks):
22302229

22312230
return result.convert_objects()
22322231

2232+
def _iterate_column_groupbys(self):
2233+
for i, colname in enumerate(self.obj.columns):
2234+
yield colname, SeriesGroupBy(self.obj.iloc[:, i], selection=colname,
2235+
grouper=self.grouper,
2236+
exclusions=self.exclusions)
2237+
2238+
def _apply_to_column_groupbys(self, func):
2239+
from pandas.tools.merge import concat
2240+
return concat((func(col_groupby)
2241+
for _, col_groupby in self._iterate_column_groupbys()),
2242+
keys=self.obj.columns,
2243+
axis=1)
2244+
2245+
def ohlc(self):
2246+
"""
2247+
Compute sum of values, excluding missing values
2248+
2249+
For multiple groupings, the result index will be a MultiIndex
2250+
"""
2251+
return self._apply_to_column_groupbys(lambda x: x._cython_agg_general('ohlc'))
22332252

22342253
from pandas.tools.plotting import boxplot_frame_groupby
22352254
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)