-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
CLN: separate raising from non-raising parts of method #27151
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
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
47dbc12
separate coerce_values from coerce_args
jbrockmendel db81995
Merge branch 'master' of https://github.com/pandas-dev/pandas into tr…
jbrockmendel 17c7cfd
Merge branch 'master' of https://github.com/pandas-dev/pandas into tr…
jbrockmendel 61047f9
Merge branch 'master' of https://github.com/pandas-dev/pandas into tr…
jbrockmendel 44a3115
Merge branch 'master' of https://github.com/pandas-dev/pandas into tr…
jbrockmendel 1334126
remove redundant method, move non-raising outside try
jbrockmendel 2ea33f9
remove ndim kwarg from make_block
jbrockmendel 85cce0f
Merge branch 'master' of https://github.com/pandas-dev/pandas into tr…
jbrockmendel ad2a613
Merge branch 'master' of https://github.com/pandas-dev/pandas into tr…
jbrockmendel 74944dd
small cleanup
jbrockmendel 2eb425f
Merge branch 'master' of https://github.com/pandas-dev/pandas into tr…
jbrockmendel c51a031
Merge branch 'master' of https://github.com/pandas-dev/pandas into tr…
jbrockmendel ccd4381
flesh out and de-dup docstrings
jbrockmendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -208,17 +208,15 @@ def array_dtype(self): | |
""" | ||
return self.dtype | ||
|
||
def make_block(self, values, placement=None, ndim=None): | ||
def make_block(self, values, placement=None): | ||
""" | ||
Create a new block, with type inference propagate any values that are | ||
not specified | ||
""" | ||
if placement is None: | ||
placement = self.mgr_locs | ||
if ndim is None: | ||
ndim = self.ndim | ||
|
||
return make_block(values, placement=placement, ndim=ndim) | ||
return make_block(values, placement=placement, ndim=self.ndim) | ||
|
||
def make_block_same_class(self, values, placement=None, ndim=None, | ||
dtype=None): | ||
|
@@ -369,7 +367,9 @@ def fillna(self, value, limit=None, inplace=False, downcast=None): | |
|
||
# fillna, but if we cannot coerce, then try again as an ObjectBlock | ||
try: | ||
values, _ = self._try_coerce_args(self.values, value) | ||
# Note: we only call try_coerce_args to let it raise | ||
self._try_coerce_args(value) | ||
|
||
blocks = self.putmask(mask, value, inplace=inplace) | ||
blocks = [b.make_block(values=self._try_coerce_result(b.values)) | ||
for b in blocks] | ||
|
@@ -659,7 +659,13 @@ def _try_cast_result(self, result, dtype=None): | |
# may need to change the dtype here | ||
return maybe_downcast_to_dtype(result, dtype) | ||
|
||
def _try_coerce_args(self, values, other): | ||
def _coerce_values(self, values): | ||
""" | ||
Coerce values (usually derived from self.values) for an operation. | ||
""" | ||
return values | ||
|
||
def _try_coerce_args(self, other): | ||
""" provide coercion to our input arguments """ | ||
|
||
if np.any(notna(other)) and not self._can_hold_element(other): | ||
|
@@ -669,7 +675,7 @@ def _try_coerce_args(self, values, other): | |
type(other).__name__, | ||
type(self).__name__.lower().replace('Block', ''))) | ||
|
||
return values, other | ||
return other | ||
|
||
def _try_coerce_result(self, result): | ||
""" reverse of try_coerce_args """ | ||
|
@@ -718,9 +724,9 @@ def replace(self, to_replace, value, inplace=False, filter=None, | |
|
||
# try to replace, if we raise an error, convert to ObjectBlock and | ||
# retry | ||
values = self._coerce_values(self.values) | ||
try: | ||
values, to_replace = self._try_coerce_args(self.values, | ||
to_replace) | ||
to_replace = self._try_coerce_args(to_replace) | ||
except (TypeError, ValueError): | ||
# GH 22083, TypeError or ValueError occurred within error handling | ||
# causes infinite loop. Cast and retry only if not objectblock. | ||
|
@@ -793,7 +799,8 @@ def setitem(self, indexer, value): | |
# coerce if block dtype can store value | ||
values = self.values | ||
try: | ||
values, value = self._try_coerce_args(values, value) | ||
value = self._try_coerce_args(value) | ||
values = self._coerce_values(values) | ||
# can keep its own dtype | ||
if hasattr(value, 'dtype') and is_dtype_equal(values.dtype, | ||
value.dtype): | ||
|
@@ -925,7 +932,7 @@ def putmask(self, mask, new, align=True, inplace=False, axis=0, | |
new = self.fill_value | ||
|
||
if self._can_hold_element(new): | ||
_, new = self._try_coerce_args(new_values, new) | ||
new = self._try_coerce_args(new) | ||
|
||
if transpose: | ||
new_values = new_values.T | ||
|
@@ -1127,7 +1134,8 @@ def _interpolate_with_fill(self, method='pad', axis=0, inplace=False, | |
return [self.copy()] | ||
|
||
values = self.values if inplace else self.values.copy() | ||
values, fill_value = self._try_coerce_args(values, fill_value) | ||
values = self._coerce_values(values) | ||
fill_value = self._try_coerce_args(fill_value) | ||
values = missing.interpolate_2d(values, method=method, axis=axis, | ||
limit=limit, fill_value=fill_value, | ||
dtype=self.dtype) | ||
|
@@ -1298,11 +1306,12 @@ def func(cond, values, other): | |
if cond.ravel().all(): | ||
return values | ||
|
||
values, other = self._try_coerce_args(values, other) | ||
values = self._coerce_values(values) | ||
other = self._try_coerce_args(other) | ||
|
||
try: | ||
return self._try_coerce_result(expressions.where( | ||
cond, values, other)) | ||
fastres = expressions.where(cond, values, other) | ||
return self._try_coerce_result(fastres) | ||
except Exception as detail: | ||
if errors == 'raise': | ||
raise TypeError( | ||
|
@@ -1349,10 +1358,10 @@ def func(cond, values, other): | |
result_blocks = [] | ||
for m in [mask, ~mask]: | ||
if m.any(): | ||
r = self._try_cast_result(result.take(m.nonzero()[0], | ||
axis=axis)) | ||
result_blocks.append( | ||
self.make_block(r.T, placement=self.mgr_locs[m])) | ||
taken = result.take(m.nonzero()[0], axis=axis) | ||
r = self._try_cast_result(taken) | ||
nb = self.make_block(r.T, placement=self.mgr_locs[m]) | ||
result_blocks.append(nb) | ||
|
||
return result_blocks | ||
|
||
|
@@ -1423,7 +1432,7 @@ def quantile(self, qs, interpolation='linear', axis=0): | |
values = values[None, :] | ||
else: | ||
values = self.get_values() | ||
values, _ = self._try_coerce_args(values, values) | ||
values = self._coerce_values(values) | ||
|
||
is_empty = values.shape[axis] == 0 | ||
orig_scalar = not is_list_like(qs) | ||
|
@@ -1575,7 +1584,8 @@ def putmask(self, mask, new, align=True, inplace=False, axis=0, | |
# use block's copy logic. | ||
# .values may be an Index which does shallow copy by default | ||
new_values = self.values if inplace else self.copy().values | ||
new_values, new = self._try_coerce_args(new_values, new) | ||
new_values = self._coerce_values(new_values) | ||
new = self._try_coerce_args(new) | ||
|
||
if isinstance(new, np.ndarray) and len(new) == len(mask): | ||
new = new[mask] | ||
|
@@ -2116,25 +2126,28 @@ def _can_hold_element(self, element): | |
return (is_integer(element) or isinstance(element, datetime) or | ||
isna(element)) | ||
|
||
def _try_coerce_args(self, values, other): | ||
def _coerce_values(self, values): | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can omit the doc-strings (as will inherit from super class) |
||
Coerce values and other to dtype 'i8'. NaN and NaT convert to | ||
Coerce values (usually derived from self.values) for an operation. | ||
""" | ||
return values.view('i8') | ||
|
||
def _try_coerce_args(self, other): | ||
""" | ||
Coerce other to dtype 'i8'. NaN and NaT convert to | ||
the smallest i8, and will correctly round-trip to NaT if converted | ||
back in _try_coerce_result. values is always ndarray-like, other | ||
may not be | ||
|
||
Parameters | ||
---------- | ||
values : ndarray-like | ||
other : ndarray-like or scalar | ||
|
||
Returns | ||
------- | ||
base-type values, base-type other | ||
base-type other | ||
""" | ||
|
||
values = values.view('i8') | ||
|
||
if isinstance(other, bool): | ||
raise TypeError | ||
elif is_null_datetimelike(other): | ||
|
@@ -2152,7 +2165,7 @@ def _try_coerce_args(self, values, other): | |
# let higher levels handle | ||
raise TypeError(other) | ||
|
||
return values, other | ||
return other | ||
|
||
def _try_coerce_result(self, result): | ||
""" reverse of try_coerce_args """ | ||
|
@@ -2245,13 +2258,6 @@ def is_view(self): | |
# check the ndarray values of the DatetimeIndex values | ||
return self.values._data.base is not None | ||
|
||
def copy(self, deep=True): | ||
""" copy constructor """ | ||
values = self.values | ||
if deep: | ||
values = values.copy() | ||
return self.make_block_same_class(values) | ||
|
||
def get_values(self, dtype=None): | ||
""" | ||
Returns an ndarray of values. | ||
|
@@ -2301,21 +2307,25 @@ def _slice(self, slicer): | |
return self.values[loc] | ||
return self.values[slicer] | ||
|
||
def _try_coerce_args(self, values, other): | ||
def _coerce_values(self, values): | ||
""" | ||
Coerce values (usually derived from self.values) for an operation. | ||
""" | ||
# asi8 is a view, needs copy | ||
return _block_shape(values.view("i8"), ndim=self.ndim) | ||
|
||
def _try_coerce_args(self, other): | ||
""" | ||
localize and return i8 for the values | ||
|
||
Parameters | ||
---------- | ||
values : ndarray-like | ||
other : ndarray-like or scalar | ||
|
||
Returns | ||
------- | ||
base-type values, base-type other | ||
base-type other | ||
""" | ||
# asi8 is a view, needs copy | ||
values = _block_shape(values.view("i8"), ndim=self.ndim) | ||
|
||
if isinstance(other, ABCSeries): | ||
other = self._holder(other) | ||
|
@@ -2343,7 +2353,7 @@ def _try_coerce_args(self, values, other): | |
else: | ||
raise TypeError(other) | ||
|
||
return values, other | ||
return other | ||
|
||
def _try_coerce_result(self, result): | ||
""" reverse of try_coerce_args """ | ||
|
@@ -2484,21 +2494,25 @@ def fillna(self, value, **kwargs): | |
value = Timedelta(value, unit='s') | ||
return super().fillna(value, **kwargs) | ||
|
||
def _try_coerce_args(self, values, other): | ||
def _coerce_values(self, values): | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can omit doc-string |
||
Coerce values (usually derived from self.values) for an operation. | ||
""" | ||
return values.view('i8') | ||
|
||
def _try_coerce_args(self, other): | ||
""" | ||
Coerce values and other to int64, with null values converted to | ||
iNaT. values is always ndarray-like, other may not be | ||
|
||
Parameters | ||
---------- | ||
values : ndarray-like | ||
other : ndarray-like or scalar | ||
|
||
Returns | ||
------- | ||
base-type values, base-type other | ||
base-type other | ||
""" | ||
values = values.view('i8') | ||
|
||
if isinstance(other, bool): | ||
raise TypeError | ||
|
@@ -2513,7 +2527,7 @@ def _try_coerce_args(self, values, other): | |
# let higher levels handle | ||
raise TypeError(other) | ||
|
||
return values, other | ||
return other | ||
|
||
def _try_coerce_result(self, result): | ||
""" reverse of try_coerce_args / try_operate """ | ||
|
@@ -2684,7 +2698,7 @@ def _maybe_downcast(self, blocks, downcast=None): | |
def _can_hold_element(self, element): | ||
return True | ||
|
||
def _try_coerce_args(self, values, other): | ||
def _try_coerce_args(self, other): | ||
""" provide coercion to our input arguments """ | ||
|
||
if isinstance(other, ABCDatetimeIndex): | ||
|
@@ -2697,7 +2711,7 @@ def _try_coerce_args(self, values, other): | |
# when falling back to ObjectBlock.where | ||
other = other.astype(object) | ||
|
||
return values, other | ||
return other | ||
|
||
def should_store(self, value): | ||
return not (issubclass(value.dtype.type, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if possible can you add types (ok for future PR) and full doc-strings