Skip to content

Commit 9fc888f

Browse files
committed
BUG: fixed bug in IntBlock splitting
bug in internals.Block.putmask ; coercing unecessarily
1 parent bddfffa commit 9fc888f

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

pandas/core/internals.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -304,14 +304,15 @@ def putmask(self, mask, new, inplace=False):
304304
if self._can_hold_element(new):
305305
new = self._try_cast(new)
306306
np.putmask(new_values, mask, new)
307-
# upcast me
308-
else:
307+
308+
# maybe upcast me
309+
elif mask.any():
309310
# type of the new block
310311
if ((isinstance(new, np.ndarray) and issubclass(new.dtype, np.number)) or
311312
isinstance(new, float)):
312-
typ = float
313+
typ = np.float64
313314
else:
314-
typ = object
315+
typ = np.object_
315316

316317
# we need to exiplicty astype here to make a copy
317318
new_values = new_values.astype(typ)
@@ -515,14 +516,15 @@ def create_block(result, items, transpose = True):
515516
return make_block(result, items, self.ref_items)
516517

517518
# see if we can operate on the entire block, or need item-by-item
518-
if cond.all().any():
519+
if not self._can_hold_na:
520+
axis = cond.ndim-1
519521
result_blocks = []
520522
for item in self.items:
521523
loc = self.items.get_loc(item)
522524
item = self.items.take([loc])
523-
v = values.take([loc])
524-
c = cond.take([loc])
525-
o = other.take([loc]) if hasattr(other,'shape') else other
525+
v = values.take([loc],axis=axis)
526+
c = cond.take([loc],axis=axis)
527+
o = other.take([loc],axis=axis) if hasattr(other,'shape') else other
526528

527529
result = func(c,v,o)
528530
if len(result) == 1:

pandas/tests/test_frame.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -248,16 +248,22 @@ def test_getitem_boolean_casting(self):
248248
df = self.tsframe.copy()
249249
df['E'] = 1
250250
df['E'] = df['E'].astype('int32')
251+
df['E1'] = df['E'].copy()
251252
df['F'] = 1
252253
df['F'] = df['F'].astype('int64')
254+
df['F1'] = df['F'].copy()
255+
253256
casted = df[df>0]
254257
result = casted.get_dtype_counts()
255-
expected = Series({'float64': 4, 'int32' : 1, 'int64' : 1})
256-
257-
### when we always cast here's the result ###
258-
#expected = Series({'float64': 6 })
258+
expected = Series({'float64': 4, 'int32' : 2, 'int64' : 2})
259259
assert_series_equal(result, expected)
260260

261+
# int block splitting
262+
df.ix[1:3,['E1','F1']] = 0
263+
casted = df[df>0]
264+
result = casted.get_dtype_counts()
265+
expected = Series({'float64': 6, 'int32' : 1, 'int64' : 1})
266+
assert_series_equal(result, expected)
261267

262268
def test_getitem_boolean_list(self):
263269
df = DataFrame(np.arange(12).reshape(3, 4))
@@ -6031,6 +6037,7 @@ def _check_align(df, cond, other, check_dtypes = True):
60316037

60326038
# dtypes
60336039
# can't check dtype when other is an ndarray
6040+
60346041
if check_dtypes and not isinstance(other,np.ndarray):
60356042
self.assert_((rs.dtypes == df.dtypes).all() == True)
60366043

@@ -6066,13 +6073,15 @@ def _check_set(df, cond, check_dtypes = True):
60666073
dfi = df.copy()
60676074
econd = cond.reindex_like(df).fillna(True)
60686075
expected = dfi.mask(~econd)
6076+
6077+
#import pdb; pdb.set_trace()
60696078
dfi.where(cond, np.nan, inplace=True)
60706079
assert_frame_equal(dfi, expected)
60716080

60726081
# dtypes (and confirm upcasts)x
60736082
if check_dtypes:
60746083
for k, v in df.dtypes.iteritems():
6075-
if issubclass(v.type,np.integer):
6084+
if issubclass(v.type,np.integer) and not cond[k].all():
60766085
v = np.dtype('float64')
60776086
self.assert_(dfi[k].dtype == v)
60786087

0 commit comments

Comments
 (0)