Skip to content

Commit 52837c1

Browse files
committed
Merge branch 'where_bool_broadcast' of https://github.com/SleepingPills/pandas into SleepingPills-where_bool_broadcast
Conflicts: doc/source/release.rst
2 parents 098de41 + fdbba96 commit 52837c1

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

doc/source/release.rst

+6-3
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ pandas 0.12
7272
- support python3 (via ``PyTables 3.0.0``) (:issue:`3750`)
7373
- Add modulo operator to Series, DataFrame
7474
- Add ``date`` method to DatetimeIndex
75-
- Add ``dropna`` argument to pivot_table (:issue: `3820`)
75+
- Add ``dropna`` argument to pivot_table (:issue: `3820`)
7676
- Simplified the API and added a describe method to Categorical
7777
- ``melt`` now accepts the optional parameters ``var_name`` and ``value_name``
7878
to specify custom column names of the returned DataFrame (:issue:`3649`),
79-
thanks @hoechenberger. If ``var_name`` is not specified and ``dataframe.columns.name``
79+
thanks @hoechenberger. If ``var_name`` is not specified and ``dataframe.columns.name``
8080
is not None, then this will be used as the ``var_name`` (:issue:`4144`).
8181
- clipboard functions use pyperclip (no dependencies on Windows, alternative
8282
dependencies offered for Linux) (:issue:`3837`).
@@ -322,10 +322,13 @@ pandas 0.12
322322
object Series/Frame was not converting properly (:issue:`4119`)
323323
- Fixed bugs in multi-index selection with column multi-index and duplicates
324324
(:issue:`4145`, :issue:`4146`)
325-
- Fixed bug in the parsing of microseconds when using the ``format``
325+
- Fixed bug in the parsing of microseconds when using the ``format``
326326
argument in ``to_datetime`` (:issue:`4152`)
327327
- Fixed bug in ``PandasAutoDateLocator`` where ``invert_xaxis`` triggered
328328
incorrectly ``MilliSecondLocator`` (:issue:`3990`)
329+
- Fixed bug in ``Series.where`` where broadcasting a single element input vector
330+
to the length of the series resulted in multiplying the value
331+
inside the input (:issue:`4192`)
329332

330333
pandas 0.11.0
331334
=============

pandas/core/series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ def where(self, cond, other=nan, inplace=False):
755755
# GH 2745
756756
# treat like a scalar
757757
if len(other) == 1:
758-
other = np.array(other[0]*len(ser))
758+
other = np.array(other[0])
759759

760760
# GH 3235
761761
# match True cond to other

pandas/tests/test_series.py

+18-5
Original file line numberDiff line numberDiff line change
@@ -1090,11 +1090,6 @@ def test_where(self):
10901090
expected = Series([0,2])
10911091
assert_series_equal(s,expected)
10921092

1093-
s = Series([1,2])
1094-
s[[True, False]] = [0]
1095-
expected = Series([0,2])
1096-
assert_series_equal(s,expected)
1097-
10981093
# failures
10991094
self.assertRaises(ValueError, s.__setitem__, tuple([[[True, False]]]), [0,2,3])
11001095
self.assertRaises(ValueError, s.__setitem__, tuple([[[True, False]]]), [])
@@ -1142,6 +1137,24 @@ def test_where(self):
11421137
s = Series(np.arange(10))
11431138
mask = s > 5
11441139
self.assertRaises(ValueError, s.__setitem__, mask, ([0]*5,))
1140+
1141+
def test_where_broadcast(self):
1142+
# Test a variety of differently sized series
1143+
for size in range(2, 6):
1144+
# Test a variety of boolean indices
1145+
for selection in [np.resize([True, False, False, False, False], size), # First element should be set
1146+
np.resize([True, False], size), # Set alternating elements]
1147+
np.resize([False], size)]: # No element should be set
1148+
# Test a variety of different numbers as content
1149+
for item in [2.0, np.nan, np.finfo(np.float).max, np.finfo(np.float).min]:
1150+
# Test numpy arrays, lists and tuples as the input to be broadcast
1151+
for arr in [np.array([item]), [item], (item,)]:
1152+
data = np.arange(size, dtype=float)
1153+
s = Series(data)
1154+
s[selection] = arr
1155+
# Construct the expected series by taking the source data or item based on the selection
1156+
expected = Series([item if use_item else data[i] for i, use_item in enumerate(selection)])
1157+
assert_series_equal(s,expected)
11451158

11461159
def test_where_inplace(self):
11471160
s = Series(np.random.randn(5))

0 commit comments

Comments
 (0)