Skip to content

Commit 50ef0e6

Browse files
author
Vikram Bhandoh
committed
astype checks notnull Fixes: pandas-dev#8732
1 parent 0a2ea0a commit 50ef0e6

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

pandas/core/internals.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,10 @@ def _astype(self, dtype, copy=False, raise_on_error=True, values=None,
398398
# force the copy here
399399
if values is None:
400400
# _astype_nansafe works fine with 1-d only
401-
values = com._astype_nansafe(self.values.ravel(), dtype, copy=True)
401+
if isnull(self.values).any():
402+
values = com._astype_nansafe(self.values.ravel(), dtype, copy=True)
403+
else:
404+
values = self.values.ravel().astype(dtype)
402405
values = values.reshape(self.values.shape)
403406
newb = make_block(values,
404407
ndim=self.ndim, placement=self.mgr_locs,

pandas/tests/test_frame.py

+9
Original file line numberDiff line numberDiff line change
@@ -2141,6 +2141,15 @@ def setUp(self):
21412141
self.simple = DataFrame(arr, columns=['one', 'two', 'three'],
21422142
index=['a', 'b', 'c'])
21432143

2144+
def test_hasnulls(self):
2145+
df = tm.makeMissingDataframe()
2146+
self.assertEqual(df['A'].notnull().all(), False)
2147+
casted = df.astype(np.float64)
2148+
expected = DataFrame(df.values.astype(np.float64),
2149+
index=df.index,
2150+
columns=df.columns)
2151+
assert_frame_equal(casted, expected)
2152+
21442153
def test_get_axis(self):
21452154
f = self.frame
21462155
self.assertEqual(f._get_axis_number(0), 0)

0 commit comments

Comments
 (0)