Skip to content

Commit 7342e0f

Browse files
jbrockmendeltm9k1
authored andcommitted
CLN: simplify try_coerce_args (pandas-dev#23288)
1 parent 74b5a8f commit 7342e0f

File tree

2 files changed

+19
-35
lines changed

2 files changed

+19
-35
lines changed

pandas/core/internals/blocks.py

+17-33
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ def fillna(self, value, limit=None, inplace=False, downcast=None):
418418

419419
# fillna, but if we cannot coerce, then try again as an ObjectBlock
420420
try:
421-
values, _, _, _ = self._try_coerce_args(self.values, value)
421+
values, _ = self._try_coerce_args(self.values, value)
422422
blocks = self.putmask(mask, value, inplace=inplace)
423423
blocks = [b.make_block(values=self._try_coerce_result(b.values))
424424
for b in blocks]
@@ -745,7 +745,7 @@ def _try_coerce_args(self, values, other):
745745
type(other).__name__,
746746
type(self).__name__.lower().replace('Block', '')))
747747

748-
return values, False, other, False
748+
return values, other
749749

750750
def _try_coerce_result(self, result):
751751
""" reverse of try_coerce_args """
@@ -795,8 +795,8 @@ def replace(self, to_replace, value, inplace=False, filter=None,
795795
# try to replace, if we raise an error, convert to ObjectBlock and
796796
# retry
797797
try:
798-
values, _, to_replace, _ = self._try_coerce_args(self.values,
799-
to_replace)
798+
values, to_replace = self._try_coerce_args(self.values,
799+
to_replace)
800800
mask = missing.mask_missing(values, to_replace)
801801
if filter is not None:
802802
filtered_out = ~self.mgr_locs.isin(filter)
@@ -853,7 +853,7 @@ def setitem(self, indexer, value):
853853
# coerce if block dtype can store value
854854
values = self.values
855855
try:
856-
values, _, value, _ = self._try_coerce_args(values, value)
856+
values, value = self._try_coerce_args(values, value)
857857
# can keep its own dtype
858858
if hasattr(value, 'dtype') and is_dtype_equal(values.dtype,
859859
value.dtype):
@@ -985,7 +985,7 @@ def putmask(self, mask, new, align=True, inplace=False, axis=0,
985985
new = self.fill_value
986986

987987
if self._can_hold_element(new):
988-
_, _, new, _ = self._try_coerce_args(new_values, new)
988+
_, new = self._try_coerce_args(new_values, new)
989989

990990
if transpose:
991991
new_values = new_values.T
@@ -1193,7 +1193,7 @@ def _interpolate_with_fill(self, method='pad', axis=0, inplace=False,
11931193
return [self.copy()]
11941194

11951195
values = self.values if inplace else self.values.copy()
1196-
values, _, fill_value, _ = self._try_coerce_args(values, fill_value)
1196+
values, fill_value = self._try_coerce_args(values, fill_value)
11971197
values = missing.interpolate_2d(values, method=method, axis=axis,
11981198
limit=limit, fill_value=fill_value,
11991199
dtype=self.dtype)
@@ -1366,8 +1366,7 @@ def func(cond, values, other):
13661366
if cond.ravel().all():
13671367
return values
13681368

1369-
values, values_mask, other, other_mask = self._try_coerce_args(
1370-
values, other)
1369+
values, other = self._try_coerce_args(values, other)
13711370

13721371
try:
13731372
return self._try_coerce_result(expressions.where(
@@ -1477,7 +1476,7 @@ def quantile(self, qs, interpolation='linear', axis=0, axes=None):
14771476
"""
14781477
kw = {'interpolation': interpolation}
14791478
values = self.get_values()
1480-
values, _, _, _ = self._try_coerce_args(values, values)
1479+
values, _ = self._try_coerce_args(values, values)
14811480

14821481
def _nanpercentile1D(values, mask, q, **kw):
14831482
# mask is Union[ExtensionArray, ndarray]
@@ -1714,7 +1713,7 @@ def putmask(self, mask, new, align=True, inplace=False, axis=0,
17141713
# use block's copy logic.
17151714
# .values may be an Index which does shallow copy by default
17161715
new_values = self.values if inplace else self.copy().values
1717-
new_values, _, new, _ = self._try_coerce_args(new_values, new)
1716+
new_values, new = self._try_coerce_args(new_values, new)
17181717

17191718
if isinstance(new, np.ndarray) and len(new) == len(mask):
17201719
new = new[mask]
@@ -2129,35 +2128,28 @@ def _try_coerce_args(self, values, other):
21292128
21302129
Returns
21312130
-------
2132-
base-type values, values mask, base-type other, other mask
2131+
base-type values, base-type other
21332132
"""
2134-
2135-
values_mask = isna(values)
21362133
values = values.view('i8')
2137-
other_mask = False
21382134

21392135
if isinstance(other, bool):
21402136
raise TypeError
21412137
elif is_null_datelike_scalar(other):
21422138
other = tslibs.iNaT
2143-
other_mask = True
21442139
elif isinstance(other, Timedelta):
2145-
other_mask = isna(other)
21462140
other = other.value
21472141
elif isinstance(other, timedelta):
21482142
other = Timedelta(other).value
21492143
elif isinstance(other, np.timedelta64):
2150-
other_mask = isna(other)
21512144
other = Timedelta(other).value
21522145
elif hasattr(other, 'dtype') and is_timedelta64_dtype(other):
2153-
other_mask = isna(other)
21542146
other = other.astype('i8', copy=False).view('i8')
21552147
else:
21562148
# coercion issues
21572149
# let higher levels handle
21582150
raise TypeError
21592151

2160-
return values, values_mask, other, other_mask
2152+
return values, other
21612153

21622154
def _try_coerce_result(self, result):
21632155
""" reverse of try_coerce_args / try_operate """
@@ -2343,7 +2335,7 @@ def _try_coerce_args(self, values, other):
23432335
# to store DatetimeTZBlock as object
23442336
other = other.astype(object).values
23452337

2346-
return values, False, other, False
2338+
return values, other
23472339

23482340
def should_store(self, value):
23492341
return not (issubclass(value.dtype.type,
@@ -2682,33 +2674,29 @@ def _try_coerce_args(self, values, other):
26822674
26832675
Returns
26842676
-------
2685-
base-type values, values mask, base-type other, other mask
2677+
base-type values, base-type other
26862678
"""
26872679

2688-
values_mask = isna(values)
26892680
values = values.view('i8')
26902681

26912682
if isinstance(other, bool):
26922683
raise TypeError
26932684
elif is_null_datelike_scalar(other):
26942685
other = tslibs.iNaT
2695-
other_mask = True
26962686
elif isinstance(other, (datetime, np.datetime64, date)):
26972687
other = self._box_func(other)
26982688
if getattr(other, 'tz') is not None:
26992689
raise TypeError("cannot coerce a Timestamp with a tz on a "
27002690
"naive Block")
2701-
other_mask = isna(other)
27022691
other = other.asm8.view('i8')
27032692
elif hasattr(other, 'dtype') and is_datetime64_dtype(other):
2704-
other_mask = isna(other)
27052693
other = other.astype('i8', copy=False).view('i8')
27062694
else:
27072695
# coercion issues
27082696
# let higher levels handle
27092697
raise TypeError
27102698

2711-
return values, values_mask, other, other_mask
2699+
return values, other
27122700

27132701
def _try_coerce_result(self, result):
27142702
""" reverse of try_coerce_args """
@@ -2855,9 +2843,8 @@ def _try_coerce_args(self, values, other):
28552843
28562844
Returns
28572845
-------
2858-
base-type values, values mask, base-type other, other mask
2846+
base-type values, base-type other
28592847
"""
2860-
values_mask = _block_shape(isna(values), ndim=self.ndim)
28612848
# asi8 is a view, needs copy
28622849
values = _block_shape(values.asi8, ndim=self.ndim)
28632850

@@ -2869,11 +2856,9 @@ def _try_coerce_args(self, values, other):
28692856
elif (is_null_datelike_scalar(other) or
28702857
(lib.is_scalar(other) and isna(other))):
28712858
other = tslibs.iNaT
2872-
other_mask = True
28732859
elif isinstance(other, self._holder):
28742860
if other.tz != self.values.tz:
28752861
raise ValueError("incompatible or non tz-aware value")
2876-
other_mask = _block_shape(isna(other), ndim=self.ndim)
28772862
other = _block_shape(other.asi8, ndim=self.ndim)
28782863
elif isinstance(other, (np.datetime64, datetime, date)):
28792864
other = tslibs.Timestamp(other)
@@ -2882,12 +2867,11 @@ def _try_coerce_args(self, values, other):
28822867
# test we can have an equal time zone
28832868
if tz is None or str(tz) != str(self.values.tz):
28842869
raise ValueError("incompatible or non tz-aware value")
2885-
other_mask = isna(other)
28862870
other = other.value
28872871
else:
28882872
raise TypeError
28892873

2890-
return values, values_mask, other, other_mask
2874+
return values, other
28912875

28922876
def _try_coerce_result(self, result):
28932877
""" reverse of try_coerce_args """

pandas/tests/internals/test_internals.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -300,14 +300,14 @@ def test_try_coerce_arg(self):
300300
block = create_block('datetime', [0])
301301

302302
# coerce None
303-
none_coerced = block._try_coerce_args(block.values, None)[2]
303+
none_coerced = block._try_coerce_args(block.values, None)[1]
304304
assert pd.Timestamp(none_coerced) is pd.NaT
305305

306306
# coerce different types of date bojects
307307
vals = (np.datetime64('2010-10-10'), datetime(2010, 10, 10),
308308
date(2010, 10, 10))
309309
for val in vals:
310-
coerced = block._try_coerce_args(block.values, val)[2]
310+
coerced = block._try_coerce_args(block.values, val)[1]
311311
assert np.int64 == type(coerced)
312312
assert pd.Timestamp('2010-10-10') == pd.Timestamp(coerced)
313313

0 commit comments

Comments
 (0)