From 66433d5bb66f808b97fd91a701819878684d7a37 Mon Sep 17 00:00:00 2001 From: Avinash Pancham Date: Sat, 20 Jun 2020 12:36:15 +0200 Subject: [PATCH 1/6] TST: Ensure dtypes are set correctly for empty integer columns #24386 --- pandas/tests/dtypes/test_dtypes.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index b1fe673e9e2f1..e91409b27cf1d 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -991,3 +991,8 @@ def test_is_dtype_no_warning(check): with tm.assert_produces_warning(None): check(data["A"]) + + +def test_check_dtype_empty_column(): + data = pd.DataFrame({"a": [1, 2]}, columns=["b"], dtype=int) + assert data.b.dtype is np.dtype("int") From feb768002a4d0a747688236fadeec568fd1f1163 Mon Sep 17 00:00:00 2001 From: Avinash Pancham Date: Sat, 20 Jun 2020 14:00:58 +0200 Subject: [PATCH 2/6] Add comment to refer to GH issue tracker --- pandas/tests/dtypes/test_dtypes.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index e91409b27cf1d..748d9b9d398dc 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -994,5 +994,6 @@ def test_is_dtype_no_warning(check): def test_check_dtype_empty_column(): + # GH24386: Ensure dtypes are set correctly for empty integer columns data = pd.DataFrame({"a": [1, 2]}, columns=["b"], dtype=int) assert data.b.dtype is np.dtype("int") From 3c2f72bd7bef6d64173ff866fb3d712cef8b416b Mon Sep 17 00:00:00 2001 From: Avinash Pancham Date: Sat, 20 Jun 2020 14:04:30 +0200 Subject: [PATCH 3/6] Refactor check, use == instead of is --- pandas/tests/dtypes/test_dtypes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index 748d9b9d398dc..d8ac7ba429506 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -996,4 +996,4 @@ def test_is_dtype_no_warning(check): def test_check_dtype_empty_column(): # GH24386: Ensure dtypes are set correctly for empty integer columns data = pd.DataFrame({"a": [1, 2]}, columns=["b"], dtype=int) - assert data.b.dtype is np.dtype("int") + assert data.b.dtype == np.dtype("int") From 8c682945cf1675d9e04c5269c63fbf6257bd76e4 Mon Sep 17 00:00:00 2001 From: Avinash Pancham Date: Sat, 20 Jun 2020 14:58:24 +0200 Subject: [PATCH 4/6] Moved file to test_constructors.py and added test for other dtypes --- pandas/tests/dtypes/test_dtypes.py | 6 ------ pandas/tests/frame/test_constructors.py | 26 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index d8ac7ba429506..b1fe673e9e2f1 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -991,9 +991,3 @@ def test_is_dtype_no_warning(check): with tm.assert_produces_warning(None): check(data["A"]) - - -def test_check_dtype_empty_column(): - # GH24386: Ensure dtypes are set correctly for empty integer columns - data = pd.DataFrame({"a": [1, 2]}, columns=["b"], dtype=int) - assert data.b.dtype == np.dtype("int") diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index baac87755c6d2..61c601662a853 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -18,6 +18,7 @@ import pandas as pd from pandas import ( Categorical, + CategoricalDtype, DataFrame, Index, MultiIndex, @@ -2245,6 +2246,31 @@ def test_from_records_empty_with_nonempty_fields_gh3682(self): tm.assert_index_equal(df.index, Index([], name="id")) assert df.index.name == "id" + @pytest.mark.parametrize( + "input_value, expected_value", + [ + ("int16", np.dtype("int16")), + ("int32", np.dtype("int32")), + ("int64", np.dtype("int64")), + ("float16", np.dtype("float16")), + ("float32", np.dtype("float32")), + ("float64", np.dtype("float64")), + ("uint16", np.dtype("uint16")), + ("uint32", np.dtype("uint32")), + ("uint64", np.dtype("uint64")), + ("datetime64[ns]", np.dtype(" Date: Sat, 20 Jun 2020 15:57:51 +0200 Subject: [PATCH 5/6] Add support for more dtypes --- pandas/tests/frame/test_constructors.py | 89 +++++++++++++++++++++++-- 1 file changed, 85 insertions(+), 4 deletions(-) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index 61c601662a853..aff9332ed8307 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -21,11 +21,19 @@ CategoricalDtype, DataFrame, Index, + Int8Dtype, + Int16Dtype, + Int32Dtype, + Int64Dtype, MultiIndex, RangeIndex, Series, Timedelta, Timestamp, + UInt8Dtype, + UInt16Dtype, + UInt32Dtype, + UInt64Dtype, date_range, isna, ) @@ -2249,20 +2257,93 @@ def test_from_records_empty_with_nonempty_fields_gh3682(self): @pytest.mark.parametrize( "input_value, expected_value", [ + # Unsigned int datatype + ("uint8", np.dtype("uint8")), + ("uint16", np.dtype("uint16")), + ("uint32", np.dtype("uint32")), + ("uint64", np.dtype("uint64")), + (np.dtype("uint8"), np.dtype("uint8")), + (np.dtype("uint16"), np.dtype("uint16")), + (np.dtype("uint32"), np.dtype("uint32")), + (np.dtype("uint64"), np.dtype("uint64")), + # Unsigned ea int datatype + ("UInt8", UInt8Dtype()), + ("UInt16", UInt16Dtype()), + ("UInt32", UInt32Dtype()), + ("UInt64", UInt64Dtype()), + (UInt8Dtype(), UInt8Dtype()), + (UInt16Dtype(), UInt16Dtype()), + (UInt32Dtype(), UInt32Dtype()), + (UInt64Dtype(), UInt64Dtype()), + # Int datatype + (int, np.dtype("int64")), + ("int", np.dtype("int64")), + ("int8", np.dtype("int8")), ("int16", np.dtype("int16")), ("int32", np.dtype("int32")), ("int64", np.dtype("int64")), + (np.dtype("int8"), np.dtype("int8")), + (np.dtype("int16"), np.dtype("int16")), + (np.dtype("int32"), np.dtype("int32")), + (np.dtype("int64"), np.dtype("int64")), + # Int ea datatype + ("Int8", Int8Dtype()), + ("Int16", Int16Dtype()), + ("Int32", Int32Dtype()), + ("Int64", Int64Dtype()), + (Int8Dtype(), Int8Dtype()), + (Int16Dtype(), Int16Dtype()), + (Int32Dtype(), Int32Dtype()), + (Int64Dtype(), Int64Dtype()), + # Floats + (float, np.dtype("float64")), + ("float", np.dtype("float64")), ("float16", np.dtype("float16")), ("float32", np.dtype("float32")), ("float64", np.dtype("float64")), - ("uint16", np.dtype("uint16")), - ("uint32", np.dtype("uint32")), - ("uint64", np.dtype("uint64")), + ("Float16", np.dtype("float16")), + ("Float32", np.dtype("float32")), + ("Float64", np.dtype("float64")), + (np.dtype("float16"), np.dtype("float16")), + (np.dtype("float32"), np.dtype("float32")), + (np.dtype("float64"), np.dtype("float64")), + # Complex + (complex, np.dtype("complex128")), + ("complex", np.dtype("complex128")), + ("complex64", np.dtype("complex64")), + ("complex128", np.dtype("complex128")), + ("complex256", np.dtype("complex256")), + ("Complex64", np.dtype("complex128")), + ("Complex128", np.dtype("complex256")), + (np.dtype("complex64"), np.dtype("complex64")), + (np.dtype("complex128"), np.dtype("complex128")), + (np.dtype("complex256"), np.dtype("complex256")), + # Strings + (str, np.dtype("O")), + (bytes, np.dtype("O")), + ("str", np.dtype("O")), + ("bytes", np.dtype("O")), + ("U", np.dtype("O")), + ("object", np.dtype("O")), + ("bytes", np.dtype("O")), + (np.dtype("O"), np.dtype("O")), + # Datetime-like objects + ("M8[ns]", np.dtype(" Date: Sat, 20 Jun 2020 17:12:19 +0200 Subject: [PATCH 6/6] Refactor testing for data types using containers in _testing.py --- pandas/tests/frame/test_constructors.py | 124 +++++------------------- 1 file changed, 22 insertions(+), 102 deletions(-) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index aff9332ed8307..6207b270faefc 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -18,22 +18,13 @@ import pandas as pd from pandas import ( Categorical, - CategoricalDtype, DataFrame, Index, - Int8Dtype, - Int16Dtype, - Int32Dtype, - Int64Dtype, MultiIndex, RangeIndex, Series, Timedelta, Timestamp, - UInt8Dtype, - UInt16Dtype, - UInt32Dtype, - UInt64Dtype, date_range, isna, ) @@ -2255,102 +2246,31 @@ def test_from_records_empty_with_nonempty_fields_gh3682(self): assert df.index.name == "id" @pytest.mark.parametrize( - "input_value, expected_value", - [ - # Unsigned int datatype - ("uint8", np.dtype("uint8")), - ("uint16", np.dtype("uint16")), - ("uint32", np.dtype("uint32")), - ("uint64", np.dtype("uint64")), - (np.dtype("uint8"), np.dtype("uint8")), - (np.dtype("uint16"), np.dtype("uint16")), - (np.dtype("uint32"), np.dtype("uint32")), - (np.dtype("uint64"), np.dtype("uint64")), - # Unsigned ea int datatype - ("UInt8", UInt8Dtype()), - ("UInt16", UInt16Dtype()), - ("UInt32", UInt32Dtype()), - ("UInt64", UInt64Dtype()), - (UInt8Dtype(), UInt8Dtype()), - (UInt16Dtype(), UInt16Dtype()), - (UInt32Dtype(), UInt32Dtype()), - (UInt64Dtype(), UInt64Dtype()), - # Int datatype - (int, np.dtype("int64")), - ("int", np.dtype("int64")), - ("int8", np.dtype("int8")), - ("int16", np.dtype("int16")), - ("int32", np.dtype("int32")), - ("int64", np.dtype("int64")), - (np.dtype("int8"), np.dtype("int8")), - (np.dtype("int16"), np.dtype("int16")), - (np.dtype("int32"), np.dtype("int32")), - (np.dtype("int64"), np.dtype("int64")), - # Int ea datatype - ("Int8", Int8Dtype()), - ("Int16", Int16Dtype()), - ("Int32", Int32Dtype()), - ("Int64", Int64Dtype()), - (Int8Dtype(), Int8Dtype()), - (Int16Dtype(), Int16Dtype()), - (Int32Dtype(), Int32Dtype()), - (Int64Dtype(), Int64Dtype()), - # Floats - (float, np.dtype("float64")), - ("float", np.dtype("float64")), - ("float16", np.dtype("float16")), - ("float32", np.dtype("float32")), - ("float64", np.dtype("float64")), - ("Float16", np.dtype("float16")), - ("Float32", np.dtype("float32")), - ("Float64", np.dtype("float64")), - (np.dtype("float16"), np.dtype("float16")), - (np.dtype("float32"), np.dtype("float32")), - (np.dtype("float64"), np.dtype("float64")), - # Complex - (complex, np.dtype("complex128")), - ("complex", np.dtype("complex128")), - ("complex64", np.dtype("complex64")), - ("complex128", np.dtype("complex128")), - ("complex256", np.dtype("complex256")), - ("Complex64", np.dtype("complex128")), - ("Complex128", np.dtype("complex256")), - (np.dtype("complex64"), np.dtype("complex64")), - (np.dtype("complex128"), np.dtype("complex128")), - (np.dtype("complex256"), np.dtype("complex256")), - # Strings - (str, np.dtype("O")), - (bytes, np.dtype("O")), - ("str", np.dtype("O")), - ("bytes", np.dtype("O")), - ("U", np.dtype("O")), - ("object", np.dtype("O")), - ("bytes", np.dtype("O")), - (np.dtype("O"), np.dtype("O")), - # Datetime-like objects - ("M8[ns]", np.dtype("