Skip to content

Commit 69fe8b3

Browse files
committed
Merge pull request #8322 from jreback/dtype
BUG: Bug in casting when setting a column in a same-dtype block (GH7704)
2 parents 362db19 + 015e4d9 commit 69fe8b3

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

doc/source/v0.15.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ Bug Fixes
912912
a custom line terminator or ``delim_whitespace=True`` (:issue:`8122`).
913913

914914
- Bug in ``read_html`` where empty tables caused a ``StopIteration`` (:issue:`7575`)
915-
915+
- Bug in casting when setting a column in a same-dtype block (:issue:`7704`)
916916
- Bug in accessing groups from a ``GroupBy`` when the original grouper
917917
was a tuple (:issue:`8121`).
918918
- Bug in ``.at`` that would accept integer indexers on a non-integer index and do fallback (:issue:`7814`)

pandas/core/internals.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,15 @@ def setitem(self, indexer, value):
556556
else:
557557
dtype = 'infer'
558558
values = self._try_coerce_and_cast_result(values, dtype)
559-
return [make_block(transf(values),
559+
block = make_block(transf(values),
560560
ndim=self.ndim, placement=self.mgr_locs,
561-
fastpath=True)]
561+
fastpath=True)
562+
563+
# may have to soft convert_objects here
564+
if block.is_object and not self.is_object:
565+
block = block.convert(convert_numeric=False)
566+
567+
return block
562568
except (ValueError, TypeError) as detail:
563569
raise
564570
except Exception as detail:

pandas/tests/test_frame.py

+9
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,15 @@ def test_setitem_cast(self):
534534
self.frame['something'] = 2.5
535535
self.assertEqual(self.frame['something'].dtype, np.float64)
536536

537+
# GH 7704
538+
# dtype conversion on setting
539+
df = DataFrame(np.random.rand(30, 3), columns=tuple('ABC'))
540+
df['event'] = np.nan
541+
df.loc[10,'event'] = 'foo'
542+
result = df.get_dtype_counts().order()
543+
expected = Series({'float64' : 3, 'object' : 1 }).order()
544+
assert_series_equal(result, expected)
545+
537546
def test_setitem_boolean_column(self):
538547
expected = self.frame.copy()
539548
mask = self.frame['A'] > 0

0 commit comments

Comments
 (0)