From 0c974457e99f0b2641c694367ce004f8b3d6fafc Mon Sep 17 00:00:00 2001 From: Vladislav Date: Mon, 11 Feb 2019 12:04:08 +0300 Subject: [PATCH 01/17] BUG: support casting from bool array to EA Integer dtype Fixes #25211. Cast boolean array to int before casting to EA Integer dtype. --- pandas/core/arrays/integer.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/core/arrays/integer.py b/pandas/core/arrays/integer.py index fd90aec3b5e8c..b460bebfa3ef9 100644 --- a/pandas/core/arrays/integer.py +++ b/pandas/core/arrays/integer.py @@ -187,6 +187,9 @@ def coerce_to_array(values, dtype, mask=None, copy=False): raise TypeError("{} cannot be converted to an IntegerDtype".format( values.dtype)) + elif is_bool_dtype(values): + values = np.array(values, dtype=int, copy=copy) + elif not (is_integer_dtype(values) or is_float_dtype(values)): raise TypeError("{} cannot be converted to an IntegerDtype".format( values.dtype)) From b3e1d7637904efb15d8ab47c26797ca365fead80 Mon Sep 17 00:00:00 2001 From: Vladislav Date: Tue, 12 Feb 2019 11:41:19 +0300 Subject: [PATCH 02/17] only cast bool to int if target dtype is interger type --- pandas/core/arrays/integer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/arrays/integer.py b/pandas/core/arrays/integer.py index b460bebfa3ef9..7a3c10eece763 100644 --- a/pandas/core/arrays/integer.py +++ b/pandas/core/arrays/integer.py @@ -187,7 +187,7 @@ def coerce_to_array(values, dtype, mask=None, copy=False): raise TypeError("{} cannot be converted to an IntegerDtype".format( values.dtype)) - elif is_bool_dtype(values): + elif is_bool_dtype(values) and is_integer_dtype(dtype): values = np.array(values, dtype=int, copy=copy) elif not (is_integer_dtype(values) or is_float_dtype(values)): From c80d4cd4d22820ab610871a85e453351d74b99a0 Mon Sep 17 00:00:00 2001 From: Vladislav Date: Wed, 20 Mar 2019 12:15:59 +0300 Subject: [PATCH 03/17] add test to #25265 --- pandas/tests/arrays/test_integer.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pandas/tests/arrays/test_integer.py b/pandas/tests/arrays/test_integer.py index 67e7db5460e6d..e57f4cc928747 100644 --- a/pandas/tests/arrays/test_integer.py +++ b/pandas/tests/arrays/test_integer.py @@ -615,6 +615,17 @@ def test_to_integer_array_float(): assert result.dtype == Int64Dtype() +@pytest.mark.parametrize( + 'bool_values, int_values', + [([False, True], [0, 1], Int64Dtype), + ([False, True, np.nan], [0, 1, np.nan], Int64Dtype)]) +def test_to_integer_array_bool(bool_values, int_values, result_dtype): + result = integer_array(bool_values) + assert result.dtype == result_dtype() + expected = integer_array(int_values) + tm.assert_extension_array_equal(result, expected) + + @pytest.mark.parametrize( 'values, to_dtype, result_dtype', [ From 9c2877af49c00dd35971f0e9d0baea4a52e79384 Mon Sep 17 00:00:00 2001 From: Vladislav Date: Wed, 20 Mar 2019 12:34:17 +0300 Subject: [PATCH 04/17] fix missing parameter to the test to #25265 --- pandas/tests/arrays/test_integer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/arrays/test_integer.py b/pandas/tests/arrays/test_integer.py index e57f4cc928747..a87e9e8ff4579 100644 --- a/pandas/tests/arrays/test_integer.py +++ b/pandas/tests/arrays/test_integer.py @@ -616,7 +616,7 @@ def test_to_integer_array_float(): @pytest.mark.parametrize( - 'bool_values, int_values', + 'bool_values, int_values, result_dtype', [([False, True], [0, 1], Int64Dtype), ([False, True, np.nan], [0, 1, np.nan], Int64Dtype)]) def test_to_integer_array_bool(bool_values, int_values, result_dtype): From 697852d5be6bf185e1b34713c7e89c1cbd9ede23 Mon Sep 17 00:00:00 2001 From: Vladislav Date: Wed, 20 Mar 2019 13:06:41 +0300 Subject: [PATCH 05/17] pass dtype to integer_array to the test to #25265 --- pandas/tests/arrays/test_integer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/arrays/test_integer.py b/pandas/tests/arrays/test_integer.py index a87e9e8ff4579..1c90560dc532a 100644 --- a/pandas/tests/arrays/test_integer.py +++ b/pandas/tests/arrays/test_integer.py @@ -620,9 +620,9 @@ def test_to_integer_array_float(): [([False, True], [0, 1], Int64Dtype), ([False, True, np.nan], [0, 1, np.nan], Int64Dtype)]) def test_to_integer_array_bool(bool_values, int_values, result_dtype): - result = integer_array(bool_values) + result = integer_array(bool_values, dtype=result_dtype()) assert result.dtype == result_dtype() - expected = integer_array(int_values) + expected = integer_array(int_values, dtype=result_dtype()) tm.assert_extension_array_equal(result, expected) From c9c4d87ee99e25a2a76307e350c031d53753dccf Mon Sep 17 00:00:00 2001 From: Vladislav Date: Wed, 20 Mar 2019 16:49:59 +0300 Subject: [PATCH 06/17] add another test case --- pandas/tests/arrays/test_integer.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/arrays/test_integer.py b/pandas/tests/arrays/test_integer.py index 1c90560dc532a..982eecb5404d1 100644 --- a/pandas/tests/arrays/test_integer.py +++ b/pandas/tests/arrays/test_integer.py @@ -618,6 +618,7 @@ def test_to_integer_array_float(): @pytest.mark.parametrize( 'bool_values, int_values, result_dtype', [([False, True], [0, 1], Int64Dtype), + ([False, True], [0, 1], 'Int64'), ([False, True, np.nan], [0, 1, np.nan], Int64Dtype)]) def test_to_integer_array_bool(bool_values, int_values, result_dtype): result = integer_array(bool_values, dtype=result_dtype()) From c313ccad006d894d2ca9d794a69a4cfa59a286ff Mon Sep 17 00:00:00 2001 From: Vladislav Date: Wed, 20 Mar 2019 16:51:44 +0300 Subject: [PATCH 07/17] add note to watsnew --- doc/source/whatsnew/v0.25.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 73eb6a15a1b47..5142df7035ff7 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -211,7 +211,7 @@ Numeric - Bug in :meth:`to_numeric` in which large negative numbers were being improperly handled (:issue:`24910`) - Bug in :meth:`to_numeric` in which numbers were being coerced to float, even though ``errors`` was not ``coerce`` (:issue:`24910`) - Bug in error messages in :meth:`DataFrame.corr` and :meth:`Series.corr`. Added the possibility of using a callable. (:issue:`25729`) -- +- Fixed bug where casting all-boolean array to integer extension array failed (:issue:`25211`) - - From 7c24ea824d6089adf7bf662cc7b2eab79bea7a21 Mon Sep 17 00:00:00 2001 From: Vladislav Date: Wed, 20 Mar 2019 17:55:06 +0300 Subject: [PATCH 08/17] Fix passing result_dtype as string --- pandas/tests/arrays/test_integer.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/tests/arrays/test_integer.py b/pandas/tests/arrays/test_integer.py index 982eecb5404d1..0c5bcddb21371 100644 --- a/pandas/tests/arrays/test_integer.py +++ b/pandas/tests/arrays/test_integer.py @@ -617,13 +617,13 @@ def test_to_integer_array_float(): @pytest.mark.parametrize( 'bool_values, int_values, result_dtype', - [([False, True], [0, 1], Int64Dtype), + [([False, True], [0, 1], Int64Dtype()), ([False, True], [0, 1], 'Int64'), - ([False, True, np.nan], [0, 1, np.nan], Int64Dtype)]) + ([False, True, np.nan], [0, 1, np.nan], Int64Dtype())]) def test_to_integer_array_bool(bool_values, int_values, result_dtype): - result = integer_array(bool_values, dtype=result_dtype()) - assert result.dtype == result_dtype() - expected = integer_array(int_values, dtype=result_dtype()) + result = integer_array(bool_values, dtype=result_dtype) + assert result.dtype == result_dtype + expected = integer_array(int_values, dtype=result_dtype) tm.assert_extension_array_equal(result, expected) From 54431e4b62dc777cbdc16841661b8965cb29822b Mon Sep 17 00:00:00 2001 From: Vladislav Date: Mon, 25 Mar 2019 16:08:21 +0300 Subject: [PATCH 09/17] possible fix for integer_array with dtype=None --- pandas/core/arrays/integer.py | 2 +- pandas/core/series.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/core/arrays/integer.py b/pandas/core/arrays/integer.py index b2a7db18f7834..75720e8f2bef8 100644 --- a/pandas/core/arrays/integer.py +++ b/pandas/core/arrays/integer.py @@ -187,7 +187,7 @@ def coerce_to_array(values, dtype, mask=None, copy=False): raise TypeError("{} cannot be converted to an IntegerDtype".format( values.dtype)) - elif is_bool_dtype(values) and is_integer_dtype(dtype): + elif is_bool_dtype(values) and (is_integer_dtype(dtype) or dtype is None): values = np.array(values, dtype=int, copy=copy) elif not (is_integer_dtype(values) or is_float_dtype(values)): diff --git a/pandas/core/series.py b/pandas/core/series.py index eeeea182e6bcc..52794b6a88ef7 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -22,7 +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_string_like, is_timedelta64_dtype) + is_iterator, is_list_like, is_scalar, is_sparse, is_string_like, is_timedelta64_dtype) from pandas.core.dtypes.generic import ( ABCDataFrame, ABCDatetimeArray, ABCDatetimeIndex, ABCSeries, ABCSparseArray, ABCSparseSeries) @@ -2644,6 +2644,8 @@ def combine(self, other, func, fill_value=None): 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. From 986548f168d1ec6dfe8787fdb1fc566e9db8e78c Mon Sep 17 00:00:00 2001 From: Vladislav Date: Mon, 25 Mar 2019 17:10:28 +0300 Subject: [PATCH 10/17] fix pep8 --- pandas/core/series.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 52794b6a88ef7..44fc707310a79 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -22,7 +22,8 @@ _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_sparse, is_string_like, + is_timedelta64_dtype) from pandas.core.dtypes.generic import ( ABCDataFrame, ABCDatetimeArray, ABCDatetimeIndex, ABCSeries, ABCSparseArray, ABCSparseSeries) From c21412ca4b99f22a1cc40e327fdc380cba3a1ad4 Mon Sep 17 00:00:00 2001 From: Vladislav Date: Tue, 26 Mar 2019 11:15:41 +0300 Subject: [PATCH 11/17] add test cases with dtype=None --- pandas/tests/arrays/test_integer.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/tests/arrays/test_integer.py b/pandas/tests/arrays/test_integer.py index 0930c923fd4c4..df23f98a6e08c 100644 --- a/pandas/tests/arrays/test_integer.py +++ b/pandas/tests/arrays/test_integer.py @@ -618,7 +618,9 @@ def test_to_integer_array_float(): 'bool_values, int_values, result_dtype', [([False, True], [0, 1], Int64Dtype()), ([False, True], [0, 1], 'Int64'), - ([False, True, np.nan], [0, 1, np.nan], Int64Dtype())]) + ([False, True, np.nan], [0, 1, np.nan], Int64Dtype()), + ([False, True], [0, 1], None), + ([False, True, np.nan], [0, 1, np.nan], None)]) def test_to_integer_array_bool(bool_values, int_values, result_dtype): result = integer_array(bool_values, dtype=result_dtype) assert result.dtype == result_dtype From 775114a785592e2c98d1df56ea3e8206e27655ad Mon Sep 17 00:00:00 2001 From: Vladislav Date: Tue, 26 Mar 2019 12:24:32 +0300 Subject: [PATCH 12/17] fix tests --- pandas/tests/arrays/test_integer.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pandas/tests/arrays/test_integer.py b/pandas/tests/arrays/test_integer.py index df23f98a6e08c..3b629f7a029a5 100644 --- a/pandas/tests/arrays/test_integer.py +++ b/pandas/tests/arrays/test_integer.py @@ -615,16 +615,16 @@ def test_to_integer_array_float(): @pytest.mark.parametrize( - 'bool_values, int_values, result_dtype', - [([False, True], [0, 1], Int64Dtype()), - ([False, True], [0, 1], 'Int64'), - ([False, True, np.nan], [0, 1, np.nan], Int64Dtype()), - ([False, True], [0, 1], None), - ([False, True, np.nan], [0, 1, np.nan], None)]) -def test_to_integer_array_bool(bool_values, int_values, result_dtype): - result = integer_array(bool_values, dtype=result_dtype) - assert result.dtype == result_dtype - expected = integer_array(int_values, dtype=result_dtype) + 'bool_values, int_values, target_dtype, expected_dtype', + [([False, True], [0, 1], Int64Dtype(), Int64Dtype()), + ([False, True], [0, 1], 'Int64', Int64Dtype()), + ([False, True, np.nan], [0, 1, np.nan], Int64Dtype(), Int64Dtype()), + ([False, True], [0, 1], None, Int64Dtype()), + ([False, True, np.nan], [0, 1, np.nan], None, Int64Dtype())]) +def test_to_integer_array_bool(bool_values, int_values, target_dtype, expected_dtype): + result = integer_array(bool_values, dtype=target_dtype) + assert result.dtype == expected_dtype + expected = integer_array(int_values, dtype=target_dtype) tm.assert_extension_array_equal(result, expected) From cb207aac2179075f092fd068176ac48314a5c951 Mon Sep 17 00:00:00 2001 From: Vladislav Date: Tue, 26 Mar 2019 12:58:28 +0300 Subject: [PATCH 13/17] fix pep8 --- pandas/tests/arrays/test_integer.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/tests/arrays/test_integer.py b/pandas/tests/arrays/test_integer.py index 3b629f7a029a5..dabfc84685666 100644 --- a/pandas/tests/arrays/test_integer.py +++ b/pandas/tests/arrays/test_integer.py @@ -621,7 +621,8 @@ def test_to_integer_array_float(): ([False, True, np.nan], [0, 1, np.nan], Int64Dtype(), Int64Dtype()), ([False, True], [0, 1], None, Int64Dtype()), ([False, True, np.nan], [0, 1, np.nan], None, Int64Dtype())]) -def test_to_integer_array_bool(bool_values, int_values, target_dtype, expected_dtype): +def test_to_integer_array_bool(bool_values, int_values, target_dtype, + expected_dtype): result = integer_array(bool_values, dtype=target_dtype) assert result.dtype == expected_dtype expected = integer_array(int_values, dtype=target_dtype) From e6655bcfe368d29c9c33c53c6ef09c77603319a6 Mon Sep 17 00:00:00 2001 From: Vladislav Date: Wed, 10 Apr 2019 13:29:46 +0300 Subject: [PATCH 14/17] quickfix failing s.combine on extention array --- pandas/core/series.py | 10 ++++------ pandas/tests/extension/base/methods.py | 6 ++++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 44fc707310a79..cb2dd9e8b9491 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -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) @@ -2635,6 +2634,7 @@ 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 @@ -2642,23 +2642,21 @@ def combine(self, other, func, fill_value=None): 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): diff --git a/pandas/tests/extension/base/methods.py b/pandas/tests/extension/base/methods.py index 1852edaa9e748..a326dce85e0f6 100644 --- a/pandas/tests/extension/base/methods.py +++ b/pandas/tests/extension/base/methods.py @@ -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): From 96fa49327b312ecd67c7021cb6a668cdb42ca297 Mon Sep 17 00:00:00 2001 From: Vladislav Date: Thu, 11 Apr 2019 11:11:35 +0300 Subject: [PATCH 15/17] fix for infering dtype in failing s.combine on extention array --- pandas/core/series.py | 4 ++-- pandas/tests/extension/base/methods.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index cb2dd9e8b9491..509aff31314a8 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -2634,7 +2634,6 @@ 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 @@ -2642,13 +2641,14 @@ def combine(self, other, func, fill_value=None): 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_extension_array_dtype(self.values): # The function can return something of any type, so check # if the type is compatible with the calling EA. + non_na_values = [v for v in new_values if notna(v)] + new_dtype, _ = infer_dtype_from(non_na_values, pandas_dtype=True) try: new_values = self._values._from_sequence(new_values, dtype=new_dtype) except Exception: diff --git a/pandas/tests/extension/base/methods.py b/pandas/tests/extension/base/methods.py index a326dce85e0f6..4092a607c351e 100644 --- a/pandas/tests/extension/base/methods.py +++ b/pandas/tests/extension/base/methods.py @@ -164,14 +164,14 @@ def test_combine_add(self, data_repeated): orig_data1._from_sequence([a + b for (a, b) in zip(list(orig_data1), list(orig_data2))], - dtype=s1.dtype)) + 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)], - dtype=s1.dtype)) + dtype=s1.dtype)) self.assert_series_equal(result, expected) def test_combine_first(self, data): From 089a72a1f9d3812ad2aa083c264426c9f3395164 Mon Sep 17 00:00:00 2001 From: Vladislav Date: Thu, 11 Apr 2019 11:15:26 +0300 Subject: [PATCH 16/17] fix for infering dtype in failing s.combine on extention array --- pandas/core/series.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 509aff31314a8..8a48e8cebaf1d 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -18,6 +18,7 @@ from pandas.util._decorators import Appender, Substitution, deprecate from pandas.util._validators import validate_bool_kwarg +from pandas.core.dtypes.cast import infer_dtype_from from pandas.core.dtypes.common import ( _is_unorderable_exception, ensure_platform_int, is_bool, is_categorical_dtype, is_datetime64_dtype, is_datetimelike, is_dict_like, @@ -2648,7 +2649,10 @@ def combine(self, other, func, fill_value=None): # The function can return something of any type, so check # if the type is compatible with the calling EA. non_na_values = [v for v in new_values if notna(v)] - new_dtype, _ = infer_dtype_from(non_na_values, pandas_dtype=True) + if non_na_values: + new_dtype, _ = infer_dtype_from(non_na_values, pandas_dtype=True) + else: + new_dtype = self.dtype try: new_values = self._values._from_sequence(new_values, dtype=new_dtype) except Exception: From 3bce8a1e45a4e8fcdf91e137722e903d75483c73 Mon Sep 17 00:00:00 2001 From: Vladislav Date: Tue, 14 May 2019 13:40:01 +0300 Subject: [PATCH 17/17] revert to c18cf28 --- pandas/core/arrays/integer.py | 2 +- pandas/core/series.py | 8 +------- pandas/tests/arrays/test_integer.py | 4 +--- pandas/tests/extension/base/methods.py | 6 ++---- 4 files changed, 5 insertions(+), 15 deletions(-) diff --git a/pandas/core/arrays/integer.py b/pandas/core/arrays/integer.py index 0c215088331c2..42aa6a055acca 100644 --- a/pandas/core/arrays/integer.py +++ b/pandas/core/arrays/integer.py @@ -188,7 +188,7 @@ def coerce_to_array(values, dtype, mask=None, copy=False): raise TypeError("{} cannot be converted to an IntegerDtype".format( values.dtype)) - elif is_bool_dtype(values) and (is_integer_dtype(dtype) or dtype is None): + elif is_bool_dtype(values) and is_integer_dtype(dtype): values = np.array(values, dtype=int, copy=copy) elif not (is_integer_dtype(values) or is_float_dtype(values)): diff --git a/pandas/core/series.py b/pandas/core/series.py index b610de8a6875f..f0b674596656a 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -17,7 +17,6 @@ from pandas.util._decorators import Appender, Substitution, deprecate from pandas.util._validators import validate_bool_kwarg -from pandas.core.dtypes.cast import infer_dtype_from from pandas.core.dtypes.common import ( _is_unorderable_exception, ensure_platform_int, is_bool, is_categorical_dtype, is_datetime64_dtype, is_datetimelike, is_dict_like, @@ -2663,13 +2662,8 @@ def combine(self, other, func, fill_value=None): 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. - non_na_values = [v for v in new_values if notna(v)] - if non_na_values: - new_dtype, _ = infer_dtype_from(non_na_values, pandas_dtype=True) - else: - new_dtype = self.dtype try: - new_values = self._values._from_sequence(new_values, dtype=new_dtype) + new_values = self._values._from_sequence(new_values) except Exception: # https://github.com/pandas-dev/pandas/issues/22850 # pandas has no control over what 3rd-party ExtensionArrays diff --git a/pandas/tests/arrays/test_integer.py b/pandas/tests/arrays/test_integer.py index 763285b34c268..066eadc9b68bc 100644 --- a/pandas/tests/arrays/test_integer.py +++ b/pandas/tests/arrays/test_integer.py @@ -617,9 +617,7 @@ def test_to_integer_array_float(): 'bool_values, int_values, target_dtype, expected_dtype', [([False, True], [0, 1], Int64Dtype(), Int64Dtype()), ([False, True], [0, 1], 'Int64', Int64Dtype()), - ([False, True, np.nan], [0, 1, np.nan], Int64Dtype(), Int64Dtype()), - ([False, True], [0, 1], None, Int64Dtype()), - ([False, True, np.nan], [0, 1, np.nan], None, Int64Dtype())]) + ([False, True, np.nan], [0, 1, np.nan], Int64Dtype(), Int64Dtype())]) def test_to_integer_array_bool(bool_values, int_values, target_dtype, expected_dtype): result = integer_array(bool_values, dtype=target_dtype) diff --git a/pandas/tests/extension/base/methods.py b/pandas/tests/extension/base/methods.py index 4092a607c351e..1852edaa9e748 100644 --- a/pandas/tests/extension/base/methods.py +++ b/pandas/tests/extension/base/methods.py @@ -163,15 +163,13 @@ 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))], - dtype=s1.dtype)) + list(orig_data2))])) 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)], - dtype=s1.dtype)) + orig_data1._from_sequence([a + val for a in list(orig_data1)])) self.assert_series_equal(result, expected) def test_combine_first(self, data):