Skip to content

Commit f982225

Browse files
committed
BUG: don't fillna in align if fill_value=nan, don't try filling int/boolblock, close pandas-dev#910
1 parent 764ce5e commit f982225

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

pandas/core/frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1824,7 +1824,7 @@ def _align_series(self, other, join='outer', axis=None, level=None,
18241824
left_result = DataFrame(fdata)
18251825
right_result = other if ridx is None else other.reindex(join_index)
18261826

1827-
fill_na = (fill_value is not None) or (method is not None)
1827+
fill_na = notnull(fill_value) or (method is not None)
18281828
if fill_na:
18291829
return (left_result.fillna(fill_value, method=method),
18301830
right_result.fillna(fill_value, method=method))

pandas/core/internals.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,23 +255,27 @@ def _backfill(values):
255255
# Is this even possible?
256256

257257
class FloatBlock(Block):
258+
_can_hold_na = True
258259

259260
def should_store(self, value):
260261
# when inserting a column should not coerce integers to floats
261262
# unnecessarily
262263
return issubclass(value.dtype.type, np.floating)
263264

264265
class IntBlock(Block):
266+
_can_hold_na = False
265267

266268
def should_store(self, value):
267269
return issubclass(value.dtype.type, np.integer)
268270

269271
class BoolBlock(Block):
272+
_can_hold_na = False
270273

271274
def should_store(self, value):
272275
return issubclass(value.dtype.type, np.bool_)
273276

274277
class ObjectBlock(Block):
278+
_can_hold_na = True
275279

276280
def should_store(self, value):
277281
return not issubclass(value.dtype.type,
@@ -943,6 +947,7 @@ def fillna(self, value, inplace=False):
943947
944948
"""
945949
new_blocks = [b.fillna(value, inplace=inplace)
950+
if b._can_hold_na else b
946951
for b in self.blocks]
947952
if inplace:
948953
return self

pandas/tests/test_frame.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2953,6 +2953,14 @@ def test_fillna(self):
29532953

29542954
result = self.mixed_frame.fillna(value=0)
29552955

2956+
def test_fillna_skip_certain_blocks(self):
2957+
# don't try to fill boolean, int blocks
2958+
2959+
df = DataFrame(np.random.randn(10, 4).astype(int))
2960+
2961+
# it works!
2962+
df.fillna(np.nan)
2963+
29562964
def test_fillna_inplace(self):
29572965
df = DataFrame(np.random.randn(10, 4))
29582966
df[1][:4] = np.nan
@@ -3410,6 +3418,19 @@ def test_align(self):
34103418
self.assertRaises(ValueError, self.frame.align, af.ix[0,:3],
34113419
join='inner', axis=2)
34123420

3421+
def test_align_int_fill_bug(self):
3422+
# GH #910
3423+
X = np.random.rand(10,10)
3424+
Y = np.ones((10,1),dtype=int)
3425+
df1 = DataFrame(X)
3426+
df1['0.X'] = Y.squeeze()
3427+
3428+
df2 = df1.astype(float)
3429+
3430+
result = df1 - df1.mean()
3431+
expected = df2 - df2.mean()
3432+
assert_frame_equal(result, expected)
3433+
34133434
#----------------------------------------------------------------------
34143435
# Transposing
34153436

0 commit comments

Comments
 (0)