Skip to content

BUG/API: Consistency in .where() when setting with None for both inplace in a Series (GH7939) #7940

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 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions doc/source/v0.15.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ API changes
In [3]: idx.isin(['a', 'c', 'e'], level=1)
Out[3]: array([ True, False, True, True, False, True], dtype=bool)

- Consistency in ``.where()`` when setting with ``None`` for both ``inplace=True`` and ``inplace=False`` in a Series (:issue:`7939`)

.. _whatsnew_0150.cat:

Expand Down
4 changes: 4 additions & 0 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,10 @@ def where(self, other, cond, align=True, raise_on_error=True,
values = values.T
is_transposed = True

# if we are passed a scalar None, convert it here
if not is_list_like(other) and isnull(other):
other = self.fill_value

# see if we can align cond
if not hasattr(cond, 'shape'):
raise ValueError(
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1597,6 +1597,24 @@ def test_where_inplace(self):
rs.where(cond, -s, inplace=True)
assert_series_equal(rs, s.where(cond, -s))

# GH 7939
# treatment of None different in where inplace
s1 = Series(['a', 'b', 'c'])
result = s1.where(s1 != 'a', None)

s2 = Series(['a', 'b', 'c'])
s2.where(s1 != 'a', None, inplace=True)
assert_series_equal(result, s2)

# this sets None directly, a little bit inconsistent
# but no easy way to deal with this in object arrays
s3 = Series(['a', 'b', 'c'])
s3[0] = None
s3[s3 == 'b'] = None
expected = Series([None,np.nan,'c'])
assert_series_equal(s3, expected)


def test_where_dups(self):
# GH 4550
# where crashes with dups in index
Expand Down