8000 Fixed bug in numpy.piecewise() for 0-d array handling by ericsuh · Pull Request #331 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Fixed bug in numpy.piecewise() for 0-d array handling #331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fixed bug in numpy.piecewise()
Updated numpy/lib/function_base.py to fix bug in numpy.piecewise() for 0-d
array handling and boolean indexing with scalars.
  • Loading branch information
ericsuh committed Jul 5, 2012
commit e553e1b4595a6925de54784dc6cd6543d70489c1
24 changes: 12 additions & 12 deletions numpy/lib/function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,21 +673,11 @@ def piecewise(x, condlist, funclist, *args, **kw):
"""
x = asanyarray(x)
n2 = len(funclist)
if isscalar(condlist) or \
not (isinstance(condlist[0], list) or
isinstance(condlist[0], ndarray)):
if isscalar(condlist):
condlist = [condlist]
condlist = [asarray(c, dtype=bool) for c in condlist]
n = len(condlist)
if n == n2-1: # compute the "otherwise" condition.
totlist = condlist[0]
for k in range(1, n):
totlist |= condlist[k]
condlist.append(~totlist)
n += 1
if (n != n2):
raise ValueError(
"function list and condition list must be the same")

zerod = False
# This is a hack to work around problems with NumPy's
# handling of 0-d arrays and boolean indexing with
Expand All @@ -704,6 +694,16 @@ def piecewise(x, condlist, funclist, *args, **kw):
newcondlist.append(condition)
condlist = newcondlist

if n == n2-1: # compute the "otherwise" condition.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well put spaces around the - for PEP8 compliance.

totlist = condlist[0]
for k in range(1, n):
totlist |= condlist[k]
condlist.append(~totlist)
n += 1
if (n != n2):
raise ValueError(
"function list and condition list must be the same")

y = zeros(x.shape, x.dtype)
for k in range(n):
item = funclist[k]
Expand Down
0