10000 [BUG] Sum of grouped bool has inconsistent dtype by rhshadrach · Pull Request #32894 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

[BUG] Sum of grouped bool has inconsistent dtype #32894

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 7 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

8000
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fixed import from refactor, restricted type in maybe_cast_to_extensio…
…n_array
  • Loading branch information
rhshadrach committed Mar 26, 2020
commit 9b32d0eda7d2d25352d89e558557ef56243bc6a4
2 changes: 1 addition & 1 deletion pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,7 @@ def _maybe_convert(arr):
# https://github.com/pandas-dev/pandas/issues/22850
# We catch all regular exceptions here, and fall back
# to an ndarray.
res = maybe_cast_to_extension_array(self, arr)
res = maybe_cast_to_extension_array(type(self), arr)
if not isinstance(res, type(self)):
# exception raised in _from_sequence; ensure we have ndarray
res = np.asarray(arr)
Expand Down
12 changes: 6 additions & 6 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
)
from pandas.util._validators import validate_bool_kwarg, validate_fillna_kwargs

from pandas.core.dtypes.cast import coerce_indexer_dtype, maybe_infer_to_datetimelike
from pandas.core.dtypes.cast import (
coerce_indexer_dtype,
maybe_cast_to_extension_array,
maybe_infer_to_datetimelike,
)
from pandas.core.dtypes.common import (
ensure_int64,
ensure_object,
Expand Down Expand Up @@ -47,11 +51,7 @@
from pandas.core.accessor import PandasDelegate, delegate_names
import pandas.core.algorithms as algorithms
from pandas.core.algorithms import _get_data_algo, factorize, take, take_1d, unique1d
from pandas.core.arrays.base import (
ExtensionArray,
_extension_array_shared_docs,
maybe_cast_to_extension_array,
)
from pandas.core.arrays.base import ExtensionArray, _extension_array_shared_docs
from pandas.core.base import NoNewAttributesMixin, PandasObject, _shared_docs
import pandas.core.common as com
from pandas.core.construction import array, extract_array, sanitize_array
Expand Down
7 changes: 4 additions & 3 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,13 @@ def maybe_cast_result_dtype(dtype: DtypeObj, how: str) -> DtypeObj:
return d.get((dtype, how), dtype)


def maybe_cast_to_extension_array(cls_or_instance, obj, dtype=None):
def maybe_cast_to_extension_array(cls, obj, dtype=None):
"""
Call to `_from_sequence` that returns the object unchanged on Exception.

Parameters
----------
cls_or_instance : ExtensionArray subclass or instance
cls : ExtensionArray subclass
obj : arraylike
Values to pass to cls._from_sequence
dtype : ExtensionDtype, optional
Expand All @@ -328,8 +328,9 @@ def maybe_cast_to_extension_array(cls_or_instance, obj, dtype=None):
-------
ExtensionArray or obj
"""
assert isinstance(cls, type), f"must pass a type: {cls}"
try:
result = cls_or_instance._from_sequence(obj, dtype=dtype)
result = cls._from_sequence(obj, dtype=dtype)
except Exception:
# We can't predict what downstream EA constructors may raise
result = obj
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2725,7 +2725,7 @@ def combine(self, other, func, fill_value=None) -> "Series":
# TODO: can we do this for only SparseDtype?
# The function can return something of any type, so check
# if the type is compatible with the calling EA.
new_values = maybe_cast_to_extension_array(self._values, new_values)
new_values = maybe_cast_to_extension_array(type(self._values), new_values)
return self._constructor(new_values, index=new_index, name=new_name)

def combine_first(self, other) -> "Series":
Expand Down
0