Skip to content

BUG: GH 4667 setitem error/dtype wrong with Series/Frame when setting with None #4676

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

Merged
merged 1 commit into from
Aug 26, 2013
Merged
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/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ See :ref:`Internal Refactoring<whatsnew_0130.refactoring>`
- Fix an issue with CacheableOffset not properly being used by many DateOffset; this prevented
the DateOffset from being cached (:issue:`4609`)
- Fix boolean comparison with a DataFrame on the lhs, and a list/tuple on the rhs (:issue:`4576`)
- Fix error/dtype conversion with setitem of ``None`` on ``Series/DataFrame`` (:issue:`4667`)

pandas 0.12
===========
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,10 @@ def putmask(self, mask, new, inplace=False):
mask = mask.reindex_axis(
self.items, axis=axis, copy=False).values.T

# if we are passed a scalar None, convert it here
if not is_list_like(new) and isnull(new):
new = np.nan

if self._can_hold_element(new):
new = self._try_cast(new)
np.putmask(new_values, mask, new)
Expand All @@ -578,7 +582,7 @@ def create_block(v, m, n, item, reshape=True):
""" return a new block, try to preserve dtype if possible """

# n should the length of the mask or a scalar here
if np.isscalar(n):
if not is_list_like(n):
n = np.array([n] * len(m))

# see if we are only masking values that if putted
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7867,6 +7867,14 @@ def test_where_datetime(self):
expected.loc[[0,1],'A'] = np.nan
assert_frame_equal(result,expected)

def test_where_none(self):
# GH 4667
# setting with None changes dtype
df = DataFrame({'series': Series(range(10))}).astype(float)
df[df > 7] = None
expected = DataFrame({'series': Series([0,1,2,3,4,5,6,7,np.nan,np.nan]) })
assert_frame_equal(df, expected)

def test_mask(self):
df = DataFrame(np.random.randn(5, 3))
cond = df > 0
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,19 @@ def f():
expected = Series([np.nan,np.nan,3,4])
assert_series_equal(result, expected)

# GH 4667
# setting with None changes dtype
s = Series(range(10)).astype(float)
s[8] = None
result = s[8]
self.assert_(isnull(result))

s = Series(range(10)).astype(float)
s[s > 8] = None
result = s[isnull(s)]
expected = Series(np.nan,index=[9])
assert_series_equal(result, expected)

def test_where_broadcast(self):
# Test a variety of differently sized series
for size in range(2, 6):
Expand Down