Skip to content

BUG: support casting from bool array to EA Integer dtype #25265

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 22 commits into from
May 14, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
0c97445
BUG: support casting from bool array to EA Integer dtype
vladserkoff Feb 11, 2019
b3e1d76
only cast bool to int if target dtype is interger type
vladserkoff Feb 12, 2019
ed61191
Merge branch 'master' into integer-array-from-bool
vladserkoff Mar 20, 2019
c80d4cd
add test to #25265
vladserkoff Mar 20, 2019
9c2877a
fix missing parameter to the test to #25265
vladserkoff Mar 20, 2019
697852d
pass dtype to integer_array to the test to #25265
vladserkoff Mar 20, 2019
c9c4d87
add another test case
vladserkoff Mar 20, 2019
c313cca
add note to watsnew
vladserkoff Mar 20, 2019
d0acd20
Merge branch 'master' into integer-array-from-bool
vladserkoff Mar 20, 2019
7c24ea8
Fix passing result_dtype as string
vladserkoff Mar 20, 2019
c18cf28
Merge branch 'master' into integer-array-from-bool
vladserkoff Mar 25, 2019
54431e4
possible fix for integer_array with dtype=None
vladserkoff Mar 25, 2019
986548f
fix pep8
vladserkoff Mar 25, 2019
c21412c
add test cases with dtype=None
vladserkoff Mar 26, 2019
775114a
fix tests
vladserkoff Mar 26, 2019
cb207aa
fix pep8
vladserkoff Mar 26, 2019
e6655bc
quickfix failing s.combine on extention array
vladserkoff Apr 10, 2019
96fa493
fix for infering dtype in failing s.combine on extention array
vladserkoff Apr 11, 2019
089a72a
fix for infering dtype in failing s.combine on extention array
vladserkoff Apr 11, 2019
f2e2eac
Merge branch 'master' into integer-array-from-bool
vladserkoff Apr 22, 2019
99a6678
Merge branch 'master' into integer-array-from-bool
vladserkoff May 14, 2019
3bce8a1
revert to c18cf28
vladserkoff May 14, 2019
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
10 changes: 4 additions & 6 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
_is_unorderable_exception, ensure_platform_int, is_bool,
is_categorical_dtype, is_datetime64_dtype, is_datetimelike, is_dict_like,
is_extension_array_dtype, is_extension_type, is_hashable, is_integer,
is_iterator, is_list_like, is_scalar, is_sparse, is_string_like,
is_timedelta64_dtype)
is_iterator, is_list_like, is_scalar, is_string_like, is_timedelta64_dtype)
from pandas.core.dtypes.generic import (
ABCDataFrame, ABCDatetimeArray, ABCDatetimeIndex, ABCSeries,
ABCSparseArray, ABCSparseSeries)
Expand Down Expand Up @@ -2635,30 +2634,29 @@ def combine(self, other, func, fill_value=None):
rv = other.get(idx, fill_value)
with np.errstate(all='ignore'):
new_values.append(func(lv, rv))
new_dtype = type(func(lv, rv))
else:
# Assume that other is a scalar, so apply the function for
# each element in the Series
new_index = self.index
with np.errstate(all='ignore'):
new_values = [func(lv, other) for lv in self._values]
new_name = self.name
new_dtype = type(func(self._values[0], other))

if is_categorical_dtype(self.values):
pass
elif is_bool(new_values[0]) and not is_sparse(self.values):
pass
elif is_extension_array_dtype(self.values):
# The function can return something of any type, so check
# if the type is compatible with the calling EA.
try:
new_values = self._values._from_sequence(new_values)
new_values = self._values._from_sequence(new_values, dtype=new_dtype)
except Exception:
# https://github.com/pandas-dev/pandas/issues/22850
# pandas has no control over what 3rd-party ExtensionArrays
# do in _values_from_sequence. We still want ops to work
# though, so we catch any regular Exception.
pass

return self._constructor(new_values, index=new_index, name=new_name)

def combine_first(self, other):
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/extension/base/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,15 @@ def test_combine_add(self, data_repeated):
expected = pd.Series(
orig_data1._from_sequence([a + b for (a, b) in
zip(list(orig_data1),
list(orig_data2))]))
list(orig_data2))],
dtype=s1.dtype))
self.assert_series_equal(result, expected)

val = s1.iloc[0]
result = s1.combine(val, lambda x1, x2: x1 + x2)
expected = pd.Series(
orig_data1._from_sequence([a + val for a in list(orig_data1)]))
orig_data1._from_sequence([a + val for a in list(orig_data1)],
dtype=s1.dtype))
self.assert_series_equal(result, expected)

def test_combine_first(self, data):
Expand Down