Skip to content

CLN: remove unused try_cast kwarg from ops #23258

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

Merged
merged 2 commits into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 3 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4971,20 +4971,19 @@ def _combine_match_index(self, other, func, level=None):
index=left.index, columns=self.columns,
copy=False)

def _combine_match_columns(self, other, func, level=None, try_cast=True):
def _combine_match_columns(self, other, func, level=None):
assert isinstance(other, Series)
left, right = self.align(other, join='outer', axis=1, level=level,
copy=False)
assert left.columns.equals(right.index)
return ops.dispatch_to_series(left, right, func, axis="columns")

def _combine_const(self, other, func, errors='raise', try_cast=True):
def _combine_const(self, other, func, errors='raise'):
if lib.is_scalar(other) or np.ndim(other) == 0:
return ops.dispatch_to_series(self, other, func)

new_data = self._data.eval(func=func, other=other,
errors=errors,
try_cast=try_cast)
errors=errors)
return self._constructor(new_data)

def combine(self, other, func, fill_value=None, overwrite=True):
Expand Down
10 changes: 2 additions & 8 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@ def shift(self, periods, axis=0, mgr=None):

return [self.make_block(new_values)]

def eval(self, func, other, errors='raise', try_cast=False, mgr=None):
def eval(self, func, other, errors='raise', mgr=None):
Copy link
Member Author

Choose a reason for hiding this comment

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

Given recent implementation of ops.dispatch_to_series, we only ever get here with try_cast=False, so we can remove the kwarg altogether.

"""
evaluate the block; return result block from the result

Expand All @@ -1330,8 +1330,6 @@ def eval(self, func, other, errors='raise', try_cast=False, mgr=None):
- ``raise`` : allow exceptions to be raised
- ``ignore`` : suppress exceptions. On error return original object

try_cast : try casting the results to the input type

Returns
-------
a new block, the result of the func
Expand Down Expand Up @@ -1368,7 +1366,7 @@ def eval(self, func, other, errors='raise', try_cast=False, mgr=None):
block = self.coerce_to_target_dtype(orig_other)
return block.eval(func, orig_other,
errors=errors,
try_cast=try_cast, mgr=mgr)
mgr=mgr)

# get the result, may need to transpose the other
def get_result(other):
Expand Down Expand Up @@ -1450,10 +1448,6 @@ def handle_error():
# transpose if needed
result = transf(result)

# try to cast if requested
if try_cast:
result = self._try_cast_result(result)

result = _block_shape(result, ndim=self.ndim)
return [self.make_block(result)]

Expand Down
31 changes: 14 additions & 17 deletions pandas/core/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1725,10 +1725,10 @@ def column_op(a, b):


def _combine_series_frame(self, other, func, fill_value=None, axis=None,
level=None, try_cast=True):
level=None):
"""
Apply binary operator `func` to self, other using alignment and fill
conventions determined by the fill_value, axis, level, and try_cast kwargs.
conventions determined by the fill_value, axis, and level kwargs.

Parameters
----------
Expand All @@ -1738,7 +1738,6 @@ def _combine_series_frame(self, other, func, fill_value=None, axis=None,
fill_value : object, default None
axis : {0, 1, 'columns', 'index', None}, default None
level : int or None, default None
try_cast : bool, default True

Returns
-------
Expand All @@ -1753,8 +1752,7 @@ def _combine_series_frame(self, other, func, fill_value=None, axis=None,
if axis == 0:
return self._combine_match_index(other, func, level=level)
else:
return self._combine_match_columns(other, func, level=level,
try_cast=try_cast)
return self._combine_match_columns(other, func, level=level)
else:
if not len(other):
return self * np.nan
Expand All @@ -1765,8 +1763,7 @@ def _combine_series_frame(self, other, func, fill_value=None, axis=None,
columns=self.columns)

# default axis is columns
return self._combine_match_columns(other, func, level=level,
try_cast=try_cast)
return self._combine_match_columns(other, func, level=level)


def _align_method_FRAME(left, right, axis):
Expand Down Expand Up @@ -1866,13 +1863,13 @@ def f(self, other, axis=default_axis, level=None, fill_value=None):
pass_op = op if axis in [0, "columns", None] else na_op
return _combine_series_frame(self, other, pass_op,
fill_value=fill_value, axis=axis,
level=level, try_cast=True)
level=level)
else:
if fill_value is not None:
self = self.fillna(fill_value)

pass_op = op if lib.is_scalar(other) else na_op
return self._combine_const(other, pass_op, try_cast=True)
assert np.ndim(other) == 0, other
return self._combine_const(other, op)

f.__name__ = op_name

Expand Down Expand Up @@ -1908,9 +1905,10 @@ def f(self, other, axis=default_axis, level=None):
elif isinstance(other, ABCSeries):
return _combine_series_frame(self, other, na_op,
fill_value=None, axis=axis,
level=level, try_cast=False)
level=level)
else:
return self._combine_const(other, na_op, try_cast=False)
assert np.ndim(other) == 0, other
return self._combine_const(other, na_op)

f.__name__ = op_name

Expand All @@ -1933,14 +1931,13 @@ def f(self, other):
elif isinstance(other, ABCSeries):
return _combine_series_frame(self, other, func,
fill_value=None, axis=None,
level=None, try_cast=False)
level=None)
else:

# straight boolean comparisons we want to allow all columns
# (regardless of dtype to pass thru) See #4537 for discussion.
res = self._combine_const(other, func,
errors='ignore',
try_cast=False)
errors='ignore')
return res.fillna(True).astype(bool)

f.__name__ = op_name
Expand Down Expand Up @@ -1987,13 +1984,13 @@ def f(self, other, axis=None):
self._get_axis_number(axis)

if isinstance(other, self._constructor):
return self._compare_constructor(other, na_op, try_cast=False)
return self._compare_constructor(other, na_op)
elif isinstance(other, (self._constructor_sliced, ABCDataFrame,
ABCSeries)):
raise Exception("input needs alignment for this object [{object}]"
.format(object=self._constructor))
else:
return self._combine_const(other, na_op, try_cast=False)
return self._combine_const(other, na_op)

f.__name__ = op_name

Expand Down
8 changes: 4 additions & 4 deletions pandas/core/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ def _init_matrix(self, data, axes, dtype=None, copy=False):
# ----------------------------------------------------------------------
# Comparison methods

def _compare_constructor(self, other, func, try_cast=True):
def _compare_constructor(self, other, func):
if not self._indexed_same(other):
raise Exception('Can only compare identically-labeled '
'same type objects')
Expand Down Expand Up @@ -745,13 +745,13 @@ def _combine(self, other, func, axis=0):
"{otype!s} is not supported in combine operation with "
"{selftype!s}".format(otype=type(other), selftype=type(self)))

def _combine_const(self, other, func, try_cast=True):
def _combine_const(self, other, func):
with np.errstate(all='ignore'):
new_values = func(self.values, other)
d = self._construct_axes_dict()
return self._constructor(new_values, **d)

def _combine_frame(self, other, func, axis=0, try_cast=True):
def _combine_frame(self, other, func, axis=0):
index, columns = self._get_plane_axes(axis)
axis = self._get_axis_number(axis)

Expand All @@ -770,7 +770,7 @@ def _combine_frame(self, other, func, axis=0, try_cast=True):
return self._constructor(new_values, self.items, self.major_axis,
self.minor_axis)

def _combine_panel(self, other, func, try_cast=True):
def _combine_panel(self, other, func):
items = self.items.union(other.items)
major = self.major_axis.union(other.major_axis)
minor = self.minor_axis.union(other.minor_axis)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/sparse/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ def _combine_match_index(self, other, func, level=None):
new_data, index=new_index, columns=self.columns,
default_fill_value=fill_value).__finalize__(self)

def _combine_match_columns(self, other, func, level=None, try_cast=True):
def _combine_match_columns(self, other, func, level=None):
# patched version of DataFrame._combine_match_columns to account for
# NumPy circumventing __rsub__ with float64 types, e.g.: 3.0 - series,
# where 3.0 is numpy.float64 and series is a SparseSeries. Still
Expand All @@ -642,7 +642,7 @@ def _combine_match_columns(self, other, func, level=None, try_cast=True):
new_data, index=self.index, columns=union,
default_fill_value=self.default_fill_value).__finalize__(self)

def _combine_const(self, other, func, errors='raise', try_cast=True):
def _combine_const(self, other, func, errors='raise'):
return self._apply_columns(lambda x: func(x, other))

def _reindex_index(self, index, method, copy, level, fill_value=np.nan,
Expand Down