Skip to content

Commit 203a8f9

Browse files
committed
BUG: Add copy parameter to prevent reinterpret cast of sparse
1 parent b76e5eb commit 203a8f9

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

pandas/core/internals.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ def f(m, v, i):
988988

989989
return [self.make_block(new_values, fastpath=True)]
990990

991-
def coerce_to_target_dtype(self, other):
991+
def coerce_to_target_dtype(self, other, copy=False):
992992
"""
993993
coerce the current block to a dtype compat for other
994994
we will return a block, possibly object, and not raise
@@ -1005,7 +1005,7 @@ def coerce_to_target_dtype(self, other):
10051005

10061006
if self.is_bool or is_object_dtype(dtype) or is_bool_dtype(dtype):
10071007
# we don't upcast to bool
1008-
return self.astype(object)
1008+
return self.astype(object, copy=copy)
10091009

10101010
elif ((self.is_float or self.is_complex) and
10111011
(is_integer_dtype(dtype) or is_float_dtype(dtype))):
@@ -1019,14 +1019,14 @@ def coerce_to_target_dtype(self, other):
10191019
# not a datetime
10201020
if not ((is_datetime64_dtype(dtype) or
10211021
is_datetime64tz_dtype(dtype)) and self.is_datetime):
1022-
return self.astype(object)
1022+
return self.astype(object, copy=copy)
10231023

10241024
# don't upcast timezone with different timezone or no timezone
10251025
mytz = getattr(self.dtype, 'tz', None)
10261026
othertz = getattr(dtype, 'tz', None)
10271027

10281028
if str(mytz) != str(othertz):
1029-
return self.astype(object)
1029+
return self.astype(object, copy=copy)
10301030

10311031
raise AssertionError("possible recursion in "
10321032
"coerce_to_target_dtype: {} {}".format(
@@ -1036,18 +1036,18 @@ def coerce_to_target_dtype(self, other):
10361036

10371037
# not a timedelta
10381038
if not (is_timedelta64_dtype(dtype) and self.is_timedelta):
1039-
return self.astype(object)
1039+
return self.astype(object, copy=copy)
10401040

10411041
raise AssertionError("possible recursion in "
10421042
"coerce_to_target_dtype: {} {}".format(
10431043
self, other))
10441044

10451045
try:
1046-
return self.astype(dtype)
1046+
return self.astype(dtype, copy=copy)
10471047
except (ValueError, TypeError):
10481048
pass
10491049

1050-
return self.astype(object)
1050+
return self.astype(object, copy=copy)
10511051

10521052
def interpolate(self, method='pad', axis=0, index=None, values=None,
10531053
inplace=False, limit=None, limit_direction='forward',
@@ -2636,6 +2636,9 @@ class SparseBlock(NonConsolidatableMixIn, Block):
26362636
_ftype = 'sparse'
26372637
_holder = SparseArray
26382638

2639+
def coerce_to_target_dtype(self, other, copy=True):
2640+
return super(SparseBlock, self).coerce_to_target_dtype(other, copy)
2641+
26392642
@property
26402643
def shape(self):
26412644
return (len(self.mgr_locs), self.sp_index.length)

pandas/core/sparse/array.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def _simple_new(cls, data, sp_index, fill_value):
248248
sp_index.ngaps > 0):
249249
# if float fill_value is being included in dense repr,
250250
# convert values to float
251-
data = data.astype(float)
251+
data = data.astype(float, copy=True)
252252

253253
result = data.view(cls)
254254

0 commit comments

Comments
 (0)