-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
CLN: simplify maybe_convert_objects, soft_convert_objects #27444
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
Changes from all commits
bc1c21a
bb7cb11
6f28f0b
c0be483
dc3d1a1
aafb5f3
37942b1
ebc8b33
b85662f
7137e98
9636301
b17489a
1ad3f76
2e3eb9d
b32f652
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,6 @@ | |
find_common_type, | ||
infer_dtype_from, | ||
infer_dtype_from_scalar, | ||
maybe_convert_objects, | ||
maybe_downcast_to_dtype, | ||
maybe_infer_dtype_type, | ||
maybe_promote, | ||
|
@@ -669,7 +668,14 @@ def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs): | |
) | ||
return newb | ||
|
||
def convert(self, copy=True, **kwargs): | ||
def convert( | ||
self, | ||
copy: bool = True, | ||
datetime: bool = True, | ||
numeric: bool = True, | ||
timedelta: bool = True, | ||
coerce: bool = False, | ||
): | ||
""" attempt to coerce any object types to better types return a copy | ||
of the block (if copy = True) by definition we are not an ObjectBlock | ||
here! | ||
|
@@ -827,9 +833,7 @@ def replace( | |
convert=convert, | ||
) | ||
if convert: | ||
blocks = [ | ||
b.convert(by_item=True, numeric=False, copy=not inplace) for b in blocks | ||
] | ||
blocks = [b.convert(numeric=False, copy=not inplace) for b in blocks] | ||
return blocks | ||
|
||
def _replace_single(self, *args, **kwargs): | ||
|
@@ -2779,45 +2783,39 @@ def is_bool(self): | |
""" | ||
return lib.is_bool_array(self.values.ravel()) | ||
|
||
# TODO: Refactor when convert_objects is removed since there will be 1 path | ||
def convert(self, *args, **kwargs): | ||
def convert( | ||
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. side issue is that a lot of this can be cleaned up now that convert_objects is gone 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. I think that deprecation was before I got here, so it isn't clear to me what else can be cleaned up 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. maybe just remove the comment, you have cleaned up reasonably. you can try looking in history but if not ok. |
||
self, | ||
copy: bool = True, | ||
datetime: bool = True, | ||
numeric: bool = True, | ||
timedelta: bool = True, | ||
coerce: bool = False, | ||
): | ||
""" attempt to coerce any object types to better types return a copy of | ||
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. add typing to the returns would be good here (and maybe Paramters / Returns to the doc-string), but can be in the future. I think adding doc-strings on routines that we are most likely going to keep is worth the effort (so use your judgement on this). |
||
the block (if copy = True) by definition we ARE an ObjectBlock!!!!! | ||
|
||
can return multiple blocks! | ||
""" | ||
|
||
if args: | ||
raise NotImplementedError | ||
by_item = kwargs.get("by_item", True) | ||
|
||
new_inputs = ["coerce", "datetime", "numeric", "timedelta"] | ||
new_style = False | ||
for kw in new_inputs: | ||
new_style |= kw in kwargs | ||
|
||
if new_style: | ||
fn = soft_convert_objects | ||
fn_inputs = new_inputs | ||
else: | ||
fn = maybe_convert_objects | ||
fn_inputs = ["convert_dates", "convert_numeric", "convert_timedeltas"] | ||
fn_inputs += ["copy"] | ||
|
||
fn_kwargs = {key: kwargs[key] for key in fn_inputs if key in kwargs} | ||
|
||
# operate column-by-column | ||
def f(m, v, i): | ||
shape = v.shape | ||
values = fn(v.ravel(), **fn_kwargs) | ||
values = soft_convert_objects( | ||
v.ravel(), | ||
datetime=datetime, | ||
numeric=numeric, | ||
timedelta=timedelta, | ||
coerce=coerce, | ||
copy=copy, | ||
) | ||
if isinstance(values, np.ndarray): | ||
# TODO: allow EA once reshape is supported | ||
values = values.reshape(shape) | ||
|
||
values = _block_shape(values, ndim=self.ndim) | ||
return values | ||
|
||
if by_item and not self._is_single_block: | ||
if self.ndim == 2: | ||
blocks = self.split_and_operate(None, f, False) | ||
else: | ||
values = f(None, self.values.ravel(), None) | ||
|
@@ -3041,7 +3039,7 @@ def re_replacer(s): | |
# convert | ||
block = self.make_block(new_values) | ||
if convert: | ||
block = block.convert(by_item=True, numeric=False) | ||
block = block.convert(numeric=False) | ||
return block | ||
|
||
def _replace_coerce( | ||
|
@@ -3080,9 +3078,7 @@ def _replace_coerce( | |
mask=mask, | ||
) | ||
if convert: | ||
block = [ | ||
b.convert(by_item=True, numeric=False, copy=True) for b in block | ||
] | ||
block = [b.convert(numeric=False, copy=True) for b in block] | ||
return block | ||
return self | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.