Skip to content

Commit bd7c7e8

Browse files
committed
BUG: GH 4667 setitem error/dtype wrong with Series/Frame when setting with None
1 parent ae2e3a4 commit bd7c7e8

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ See :ref:`Internal Refactoring<whatsnew_0130.refactoring>`
285285
- Fix an issue with CacheableOffset not properly being used by many DateOffset; this prevented
286286
the DateOffset from being cached (:issue:`4609`)
287287
- Fix boolean comparison with a DataFrame on the lhs, and a list/tuple on the rhs (:issue:`4576`)
288+
- Fix error/dtype conversion with setitem of ``None`` on ``Series/DataFrame`` (:issue:`4667`)
288289

289290
pandas 0.12
290291
===========

pandas/core/internals.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,10 @@ def putmask(self, mask, new, inplace=False):
564564
mask = mask.reindex_axis(
565565
self.items, axis=axis, copy=False).values.T
566566

567+
# if we are passed a scalar None, convert it here
568+
if not is_list_like(new) and isnull(new):
569+
new = np.nan
570+
567571
if self._can_hold_element(new):
568572
new = self._try_cast(new)
569573
np.putmask(new_values, mask, new)
@@ -578,7 +582,7 @@ def create_block(v, m, n, item, reshape=True):
578582
""" return a new block, try to preserve dtype if possible """
579583

580584
# n should the length of the mask or a scalar here
581-
if np.isscalar(n):
585+
if not is_list_like(n):
582586
n = np.array([n] * len(m))
583587

584588
# see if we are only masking values that if putted

pandas/tests/test_frame.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7867,6 +7867,14 @@ def test_where_datetime(self):
78677867
expected.loc[[0,1],'A'] = np.nan
78687868
assert_frame_equal(result,expected)
78697869

7870+
def test_where_none(self):
7871+
# GH 4667
7872+
# setting with None changes dtype
7873+
df = DataFrame({'series': Series(range(10))}).astype(float)
7874+
df[df > 7] = None
7875+
expected = DataFrame({'series': Series([0,1,2,3,4,5,6,7,np.nan,np.nan]) })
7876+
assert_frame_equal(df, expected)
7877+
78707878
def test_mask(self):
78717879
df = DataFrame(np.random.randn(5, 3))
78727880
cond = df > 0

pandas/tests/test_series.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,19 @@ def f():
12491249
expected = Series([np.nan,np.nan,3,4])
12501250
assert_series_equal(result, expected)
12511251

1252+
# GH 4667
1253+
# setting with None changes dtype
1254+
s = Series(range(10)).astype(float)
1255+
s[8] = None
1256+
result = s[8]
1257+
self.assert_(isnull(result))
1258+
1259+
s = Series(range(10)).astype(float)
1260+
s[s > 8] = None
1261+
result = s[isnull(s)]
1262+
expected = Series(np.nan,index=[9])
1263+
assert_series_equal(result, expected)
1264+
12521265
def test_where_broadcast(self):
12531266
# Test a variety of differently sized series
12541267
for size in range(2, 6):

0 commit comments

Comments
 (0)