From 50ef0e60b9a6e9ed6df430abf3675d0601b33919 Mon Sep 17 00:00:00 2001 From: Vikram Bhandoh Date: Sat, 29 Nov 2014 12:31:05 +0000 Subject: [PATCH 1/2] astype checks notnull Fixes: #8732 --- pandas/core/internals.py | 5 ++++- pandas/tests/test_frame.py | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pandas/core/internals.py b/pandas/core/internals.py index ef33e27d861fd..750b47786dec3 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -398,7 +398,10 @@ def _astype(self, dtype, copy=False, raise_on_error=True, values=None, # force the copy here if values is None: # _astype_nansafe works fine with 1-d only - values = com._astype_nansafe(self.values.ravel(), dtype, copy=True) + if isnull(self.values).any(): + values = com._astype_nansafe(self.values.ravel(), dtype, copy=True) + else: + values = self.values.ravel().astype(dtype) values = values.reshape(self.values.shape) newb = make_block(values, ndim=self.ndim, placement=self.mgr_locs, diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index fc031afe728dc..da5f83935f31e 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -2141,6 +2141,15 @@ def setUp(self): self.simple = DataFrame(arr, columns=['one', 'two', 'three'], index=['a', 'b', 'c']) + def test_hasnulls(self): + df = tm.makeMissingDataframe() + self.assertEqual(df['A'].notnull().all(), False) + casted = df.astype(np.float64) + expected = DataFrame(df.values.astype(np.float64), + index=df.index, + columns=df.columns) + assert_frame_equal(casted, expected) + def test_get_axis(self): f = self.frame self.assertEqual(f._get_axis_number(0), 0) From ec8f6b5e50af4b591daceaf128dd245df4b80a76 Mon Sep 17 00:00:00 2001 From: Vikram Bhandoh Date: Sat, 29 Nov 2014 13:13:03 +0000 Subject: [PATCH 2/2] added benchmark --- pandas/tests/test_frame.py | 7 +++++++ vb_suite/astype.py | 14 ++++++++++++++ vb_suite/suite.py | 1 + 3 files changed, 22 insertions(+) create mode 100644 vb_suite/astype.py diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index da5f83935f31e..e19f5a0ff8719 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -2142,6 +2142,7 @@ def setUp(self): index=['a', 'b', 'c']) def test_hasnulls(self): + # Github Issue 8732 df = tm.makeMissingDataframe() self.assertEqual(df['A'].notnull().all(), False) casted = df.astype(np.float64) @@ -2150,6 +2151,12 @@ def test_hasnulls(self): columns=df.columns) assert_frame_equal(casted, expected) + casted = df.astype(np.str) + expected = DataFrame(df.values.astype(np.str), + index=df.index, + columns=df.columns) + assert_frame_equal(casted, expected) + def test_get_axis(self): f = self.frame self.assertEqual(f._get_axis_number(0), 0) diff --git a/vb_suite/astype.py b/vb_suite/astype.py new file mode 100644 index 0000000000000..26deab3a145cb --- /dev/null +++ b/vb_suite/astype.py @@ -0,0 +1,14 @@ +from vbench.api import Benchmark + +common_setup = """from pandas_vb_common import * +from datetime import timedelta +import numpy as np + +N = 1000000 +arr = np.random.randint(1,10,size=1000000) +s = pd.Series(arr) +""" + +astype_test = Benchmark('s.astype(np.str)', + common_setup, + name='astype_test') diff --git a/vb_suite/suite.py b/vb_suite/suite.py index a16d183ae62e2..ee5a49dac2e66 100644 --- a/vb_suite/suite.py +++ b/vb_suite/suite.py @@ -13,6 +13,7 @@ 'indexing', 'io_bench', 'io_sql', + 'astype', 'inference', 'hdfstore_bench', 'join_merge',