From b1fb8549d46a5ad1923a412dc177706d3bc36a4d Mon Sep 17 00:00:00 2001 From: taytzehao Date: Fri, 16 Apr 2021 12:11:50 +0800 Subject: [PATCH 1/6] Updated qcut for Float64DType --- pandas/core/reshape/tile.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/reshape/tile.py b/pandas/core/reshape/tile.py index 41e1ff41d9ba2..cba1a15934bb8 100644 --- a/pandas/core/reshape/tile.py +++ b/pandas/core/reshape/tile.py @@ -24,7 +24,7 @@ is_datetime_or_timedelta_dtype, is_extension_array_dtype, is_integer, - is_integer_dtype, + is_numeric_dtype, is_list_like, is_scalar, is_timedelta64_dtype, @@ -488,7 +488,7 @@ def _coerce_to_type(x): # Will properly support in the future. # https://github.com/pandas-dev/pandas/pull/31290 # https://github.com/pandas-dev/pandas/issues/31389 - elif is_extension_array_dtype(x.dtype) and is_integer_dtype(x.dtype): + elif is_extension_array_dtype(x.dtype) and is_numeric_dtype(x.dtype): x = x.to_numpy(dtype=np.float64, na_value=np.nan) if dtype is not None: From 7af63a220497541d05c51f7d3f92d541acf3a130 Mon Sep 17 00:00:00 2001 From: taytzehao Date: Fri, 16 Apr 2021 09:18:37 +0000 Subject: [PATCH 2/6] Fixes from pre-commit [automated commit] --- pandas/core/reshape/tile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/reshape/tile.py b/pandas/core/reshape/tile.py index cba1a15934bb8..7b9c3883d74e3 100644 --- a/pandas/core/reshape/tile.py +++ b/pandas/core/reshape/tile.py @@ -24,8 +24,8 @@ is_datetime_or_timedelta_dtype, is_extension_array_dtype, is_integer, - is_numeric_dtype, is_list_like, + is_numeric_dtype, is_scalar, is_timedelta64_dtype, ) From c4259aa25ebac57b4774bbfd4284f34640d70d29 Mon Sep 17 00:00:00 2001 From: taytzehao Date: Sun, 18 Apr 2021 16:23:11 +0800 Subject: [PATCH 3/6] Added test and documentation for qcut Float64DType support --- doc/source/whatsnew/v1.3.0.rst | 2 +- pandas/tests/reshape/test_qcut.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 2b0b62ab7facf..690557421f024 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -663,7 +663,7 @@ Conversion - Bug in :class:`Index` construction silently ignoring a passed ``dtype`` when the data cannot be cast to that dtype (:issue:`21311`) - Bug in :meth:`StringArray.astype` falling back to numpy and raising when converting to ``dtype='categorical'`` (:issue:`40450`) - Bug in :class:`DataFrame` construction with a dictionary containing an arraylike with ``ExtensionDtype`` and ``copy=True`` failing to make a copy (:issue:`38939`) -- +- Bug in :func:`_coerce_to_type` failing to convert ``Float64DType`` input into numpy array (:issue:`40730`) Strings ^^^^^^^ diff --git a/pandas/tests/reshape/test_qcut.py b/pandas/tests/reshape/test_qcut.py index 7996c15ae8e64..ae19ceb71e3ac 100644 --- a/pandas/tests/reshape/test_qcut.py +++ b/pandas/tests/reshape/test_qcut.py @@ -301,3 +301,19 @@ def test_qcut_nullable_integer(q, any_nullable_int_dtype): expected = qcut(arr.astype(float), q) tm.assert_categorical_equal(result, expected) + + +@pytest.mark.parametrize("Data_type,Data_type_string", + [ + (pd.Float64Dtype(),"Float64Dtype"), + (pd.Int64Dtype(),"Int64Dtype") + ] + ) +def test_qcut_numeric_dtype(Data_type,Data_type_string): + series = pd.Series([1.0, 2.0, 3.0, 4.0], dtype=Data_type) + + try: + pd.qcut(series,2) + except: + Fail_string=Data_type_string+" is not supported" + pytest.fail(msg=Fail_string ) \ No newline at end of file From 29fddabedff6bfcd5c409160a57cabd9010db719 Mon Sep 17 00:00:00 2001 From: taytzehao Date: Sun, 18 Apr 2021 16:37:56 +0800 Subject: [PATCH 4/6] Updated qcut test formatting --- pandas/tests/reshape/test_qcut.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/pandas/tests/reshape/test_qcut.py b/pandas/tests/reshape/test_qcut.py index ae19ceb71e3ac..c03d0ff3bc9e3 100644 --- a/pandas/tests/reshape/test_qcut.py +++ b/pandas/tests/reshape/test_qcut.py @@ -303,17 +303,20 @@ def test_qcut_nullable_integer(q, any_nullable_int_dtype): tm.assert_categorical_equal(result, expected) -@pytest.mark.parametrize("Data_type,Data_type_string", - [ - (pd.Float64Dtype(),"Float64Dtype"), - (pd.Int64Dtype(),"Int64Dtype") - ] - ) +@pytest.mark.parametrize( + "Data_type,Data_type_string", + [ + (pd.Float64Dtype(),"Float64Dtype"), + (pd.Int64Dtype(),"Int64Dtype") + ] +) def test_qcut_numeric_dtype(Data_type,Data_type_string): series = pd.Series([1.0, 2.0, 3.0, 4.0], dtype=Data_type) try: pd.qcut(series,2) except: - Fail_string=Data_type_string+" is not supported" - pytest.fail(msg=Fail_string ) \ No newline at end of file + Fail_string = Data_type_string + " is not supported" + pytest.fail( msg = Fail_string ) + + \ No newline at end of file From e8048b909e89ab3e12b19518d2350c3aa40a4847 Mon Sep 17 00:00:00 2001 From: taytzehao Date: Mon, 19 Apr 2021 14:58:47 +0800 Subject: [PATCH 5/6] address comment for PR #40969 --- doc/source/whatsnew/v1.3.0.rst | 2 +- pandas/tests/reshape/test_qcut.py | 23 +++-------------------- 2 files changed, 4 insertions(+), 21 deletions(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 0e8a914c92b43..4ef8fe116596f 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -695,7 +695,7 @@ Conversion - Bug in :class:`Index` construction silently ignoring a passed ``dtype`` when the data cannot be cast to that dtype (:issue:`21311`) - Bug in :meth:`StringArray.astype` falling back to numpy and raising when converting to ``dtype='categorical'`` (:issue:`40450`) - Bug in :class:`DataFrame` construction with a dictionary containing an arraylike with ``ExtensionDtype`` and ``copy=True`` failing to make a copy (:issue:`38939`) -- Bug in :func:`_coerce_to_type` failing to convert ``Float64DType`` input into numpy array (:issue:`40730`) +- Bug in :meth:`qcut` raising error when taking ``Float64DType`` as input (:issue:`40730`) Strings ^^^^^^^ diff --git a/pandas/tests/reshape/test_qcut.py b/pandas/tests/reshape/test_qcut.py index c03d0ff3bc9e3..89036f13cbacd 100644 --- a/pandas/tests/reshape/test_qcut.py +++ b/pandas/tests/reshape/test_qcut.py @@ -293,8 +293,8 @@ def test_qcut_bool_coercion_to_int(bins, box, compare): @pytest.mark.parametrize("q", [2, 5, 10]) -def test_qcut_nullable_integer(q, any_nullable_int_dtype): - arr = pd.array(np.arange(100), dtype=any_nullable_int_dtype) +def test_qcut_nullable_integer(q, any_nullable_numeric_dtype): + arr = pd.array(np.arange(100), dtype=any_nullable_numeric_dtype) arr[::2] = pd.NA result = qcut(arr, q) @@ -302,21 +302,4 @@ def test_qcut_nullable_integer(q, any_nullable_int_dtype): tm.assert_categorical_equal(result, expected) - -@pytest.mark.parametrize( - "Data_type,Data_type_string", - [ - (pd.Float64Dtype(),"Float64Dtype"), - (pd.Int64Dtype(),"Int64Dtype") - ] -) -def test_qcut_numeric_dtype(Data_type,Data_type_string): - series = pd.Series([1.0, 2.0, 3.0, 4.0], dtype=Data_type) - - try: - pd.qcut(series,2) - except: - Fail_string = Data_type_string + " is not supported" - pytest.fail( msg = Fail_string ) - - \ No newline at end of file + \ No newline at end of file From 95354c6468fe6ea61dbb5c317a565d57eb047721 Mon Sep 17 00:00:00 2001 From: taytzehao Date: Mon, 19 Apr 2021 09:50:49 +0000 Subject: [PATCH 6/6] Fixes from pre-commit [automated commit] --- pandas/tests/reshape/test_qcut.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pandas/tests/reshape/test_qcut.py b/pandas/tests/reshape/test_qcut.py index 89036f13cbacd..c12d28f6f1380 100644 --- a/pandas/tests/reshape/test_qcut.py +++ b/pandas/tests/reshape/test_qcut.py @@ -301,5 +301,3 @@ def test_qcut_nullable_integer(q, any_nullable_numeric_dtype): expected = qcut(arr.astype(float), q) tm.assert_categorical_equal(result, expected) - - \ No newline at end of file