Skip to content

astype checks notnull Fixes: #8732 #8924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2141,6 +2141,22 @@ def setUp(self):
self.simple = DataFrame(arr, columns=['one', 'two', 'three'],
index=['a', 'b', 'c'])

def test_hasnulls(self):
# Github Issue 8732
df = tm.makeMissingDataframe()
self.assertEqual(df['A'].notnull().all(), False)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the issue number as a comment here

casted = df.astype(np.float64)
expected = DataFrame(df.values.astype(np.float64),
index=df.index,
columns=df.columns)
assert_frame_equal(casted, expected)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs to be tested with astype(str) (with actual string data that has nan and say floats that has nans) as well as this test

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)
Expand Down
14 changes: 14 additions & 0 deletions vb_suite/astype.py
Original file line number Diff line number Diff line change
@@ -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')
1 change: 1 addition & 0 deletions vb_suite/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
'indexing',
'io_bench',
'io_sql',
'astype',
'inference',
'hdfstore_bench',
'join_merge',
Expand Down