Skip to content

Commit 99137af

Browse files
author
y-p
committed
Merge branch 'PR3510'
* PR3510: Fixed Unbound Variable `edge` access when BinGrouper is empty
2 parents ee4a740 + b30c37a commit 99137af

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

RELEASE.rst

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ pandas 0.11.1
7070
- applymap on a DataFrame with a non-unique index now works
7171
(removed warning) (GH2786_), and fix (GH3230_)
7272
- Fix to_csv to handle non-unique columns (GH3495_)
73+
- Fixed bug in groupby with empty series referencing a variable before assignment. (GH3510_)
7374

7475
.. _GH3164: https://github.com/pydata/pandas/issues/3164
7576
.. _GH2786: https://github.com/pydata/pandas/issues/2786
@@ -83,6 +84,7 @@ pandas 0.11.1
8384
.. _GH3426: https://github.com/pydata/pandas/issues/3426
8485
.. _GH3466: https://github.com/pydata/pandas/issues/3466
8586
.. _GH3038: https://github.com/pydata/pandas/issues/3038
87+
.. _GH3510: https://github.com/pydata/pandas/issues/3510
8688
.. _GH3437: https://github.com/pydata/pandas/issues/3437
8789
.. _GH3455: https://github.com/pydata/pandas/issues/3455
8890
.. _GH3457: https://github.com/pydata/pandas/issues/3457

pandas/core/groupby.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1012,8 +1012,8 @@ def get_iterator(self, data, axis=0):
10121012
yield label, data[start:edge]
10131013
start = edge
10141014

1015-
if edge < len(data):
1016-
yield self.binlabels[-1], data[edge:]
1015+
if start < len(data):
1016+
yield self.binlabels[-1], data[start:]
10171017
else:
10181018
start = 0
10191019
for edge, label in izip(self.bins, self.binlabels):
@@ -1022,8 +1022,8 @@ def get_iterator(self, data, axis=0):
10221022
start = edge
10231023

10241024
n = len(data.axes[axis])
1025-
if edge < n:
1026-
inds = range(edge, n)
1025+
if start < n:
1026+
inds = range(start, n)
10271027
yield self.binlabels[-1], data.take(inds, axis=axis)
10281028

10291029
def apply(self, f, data, axis=0, keep_internal=False):

pandas/tests/test_groupby.py

+10
Original file line numberDiff line numberDiff line change
@@ -2434,6 +2434,16 @@ def noddy(value, weight):
24342434
# don't die
24352435
no_toes = df_grouped.apply(lambda x: noddy(x.value, x.weight ))
24362436

2437+
def test_groupby_with_empty(self):
2438+
import pandas as pd
2439+
index = pd.DatetimeIndex(())
2440+
data = ()
2441+
series = pd.Series(data, index)
2442+
grouper = pd.tseries.resample.TimeGrouper('D')
2443+
grouped = series.groupby(grouper)
2444+
assert next(iter(grouped), None) is None
2445+
2446+
24372447
def assert_fp_equal(a, b):
24382448
assert((np.abs(a - b) < 1e-12).all())
24392449

0 commit comments

Comments
 (0)