Skip to content

REF: implement maybe_promote_scalar #29331

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 20 additions & 0 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,26 @@ def maybe_promote(dtype, fill_value=np.nan):
fill_value = np.nan
dtype = np.dtype(np.object_)

return maybe_promote_scalar(dtype, fill_value)


def maybe_promote_scalar(dtype, fill_value=np.nan):
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a doc-string / types

Copy link
Member Author

Choose a reason for hiding this comment

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

will do

Copy link
Member Author

Choose a reason for hiding this comment

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

added docstring, annotations i need to coordinate with others about implementing correct types in pd._typing

"""
Find the minimal dtype that can hold both the given dtype and fill_value.

Parameters
----------
dtype : np.dtype or ExceptionDtype
fill_value : scalar, default np.nan

Returns
-------
dtype
Upcasted from dtype argument if necessary.
fill_value
Upcasted from fill_value argument if necessary.
"""

# returns tuple of (dtype, fill_value)
if issubclass(dtype.type, np.datetime64):
if isinstance(fill_value, datetime) and fill_value.tzinfo is not None:
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
maybe_downcast_numeric,
maybe_downcast_to_dtype,
maybe_infer_dtype_type,
maybe_promote,
maybe_promote_scalar,
maybe_upcast,
soft_convert_objects,
)
Expand Down Expand Up @@ -856,7 +856,7 @@ def setitem(self, indexer, value):

# cast the values to a type that can hold nan (if necessary)
if not self._can_hold_element(value):
dtype, _ = maybe_promote(arr_value.dtype)
dtype, _ = maybe_promote_scalar(arr_value.dtype)
values = values.astype(dtype)

if transpose:
Expand Down Expand Up @@ -994,7 +994,7 @@ def f(mask, val, idx):
n = np.array(new)

# type of the new block
dtype, _ = maybe_promote(n.dtype)
dtype, _ = maybe_promote_scalar(n.dtype)

# we need to explicitly astype here to make a copy
n = n.astype(dtype)
Expand Down Expand Up @@ -3169,7 +3169,7 @@ def _putmask_preserve(nv, n):
return _putmask_preserve(v, n)

# change the dtype if needed
dtype, _ = maybe_promote(n.dtype)
dtype, _ = maybe_promote_scalar(n.dtype)

if is_extension_type(v.dtype) and is_object_dtype(dtype):
v = v._internal_get_values(dtype)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
find_common_type,
infer_dtype_from_scalar,
maybe_convert_objects,
maybe_promote,
maybe_promote_scalar,
)
from pandas.core.dtypes.common import (
_NS_DTYPE,
Expand Down Expand Up @@ -1291,7 +1291,7 @@ def _slice_take_blocks_ax0(self, slice_or_indexer, fill_tuple=None):
return [blk.getitem_block(slobj, new_mgr_locs=slice(0, sllen))]
elif not allow_fill or self.ndim == 1:
if allow_fill and fill_tuple[0] is None:
_, fill_value = maybe_promote(blk.dtype)
_, fill_value = maybe_promote_scalar(blk.dtype)
fill_tuple = (fill_value,)

return [
Expand Down