From 3eef1b64cb31724edbdd14a59e9e2b99fc37634f Mon Sep 17 00:00:00 2001 From: Dustan Levenstein Date: Sat, 17 Aug 2019 13:31:55 -0500 Subject: [PATCH 1/6] Added try-except clause to catch numpy error. Fixes https://github.com/pandas-dev/pandas/issues/27953 --- pandas/core/internals/construction.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index 3126b9d9d3e2e..b1afaf3db0479 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -223,10 +223,13 @@ def init_dict(data, index, columns, dtype=None): # no obvious "empty" int column if missing.any() and not is_integer_dtype(dtype): - if dtype is None or np.issubdtype(dtype, np.flexible): - # GH#1783 - nan_dtype = object - else: + try: + if dtype is None or np.issubdtype(dtype, np.flexible): + # GH#1783 + nan_dtype = object + else: + nan_dtype = dtype + except(TypeError): nan_dtype = dtype val = construct_1d_arraylike_from_scalar(np.nan, len(index), nan_dtype) arrays.loc[missing] = [val] * missing.sum() From 976c2b87b61b3a5cb712105c03623f75262092f9 Mon Sep 17 00:00:00 2001 From: Dustan Levenstein Date: Sat, 17 Aug 2019 14:18:07 -0500 Subject: [PATCH 2/6] Modified to use test instead of try-except. --- pandas/core/internals/construction.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index b1afaf3db0479..f28b1d3d7088b 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -36,6 +36,7 @@ ABCTimedeltaIndex, ) +from pandas.core.dtypes.dtypes import CategoricalDtype from pandas.core import algorithms, common as com from pandas.core.arrays import Categorical from pandas.core.construction import sanitize_array @@ -223,13 +224,12 @@ def init_dict(data, index, columns, dtype=None): # no obvious "empty" int column if missing.any() and not is_integer_dtype(dtype): - try: - if dtype is None or np.issubdtype(dtype, np.flexible): - # GH#1783 - nan_dtype = object - else: - nan_dtype = dtype - except(TypeError): + if isinstance(dtype, CategoricalDtype): + nan_dtype = dtype + elif dtype is None or np.issubdtype(dtype, np.flexible): + # GH#1783 + nan_dtype = object + else: nan_dtype = dtype val = construct_1d_arraylike_from_scalar(np.nan, len(index), nan_dtype) arrays.loc[missing] = [val] * missing.sum() From e8b4d89cbbcf39f46629cba30384da723220c6a4 Mon Sep 17 00:00:00 2001 From: Dustan Levenstein Date: Sat, 17 Aug 2019 15:04:08 -0500 Subject: [PATCH 3/6] Reordered import statements. --- pandas/core/internals/construction.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index f28b1d3d7088b..62a6b15171caa 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -36,10 +36,10 @@ ABCTimedeltaIndex, ) -from pandas.core.dtypes.dtypes import CategoricalDtype from pandas.core import algorithms, common as com from pandas.core.arrays import Categorical from pandas.core.construction import sanitize_array +from pandas.core.dtypes.dtypes import CategoricalDtype from pandas.core.index import ( Index, _get_objs_combined_axis, From 534d87f959848478b1705b13aa85a8376208663c Mon Sep 17 00:00:00 2001 From: Dustan Levenstein Date: Sat, 17 Aug 2019 15:42:34 -0500 Subject: [PATCH 4/6] Used isort. --- pandas/core/internals/construction.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index 62a6b15171caa..22e599d293fc7 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -27,6 +27,7 @@ is_list_like, is_object_dtype, ) +from pandas.core.dtypes.dtypes import CategoricalDtype from pandas.core.dtypes.generic import ( ABCDataFrame, ABCDatetimeIndex, @@ -39,7 +40,6 @@ from pandas.core import algorithms, common as com from pandas.core.arrays import Categorical from pandas.core.construction import sanitize_array -from pandas.core.dtypes.dtypes import CategoricalDtype from pandas.core.index import ( Index, _get_objs_combined_axis, From 7b5d4cdfb60d438aff6595825dfc0979232013eb Mon Sep 17 00:00:00 2001 From: Dustan Levenstein Date: Sun, 18 Aug 2019 12:25:49 -0500 Subject: [PATCH 5/6] jbrockmendel's recommended change. --- pandas/core/internals/construction.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index 22e599d293fc7..51535db9208a0 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -27,7 +27,6 @@ is_list_like, is_object_dtype, ) -from pandas.core.dtypes.dtypes import CategoricalDtype from pandas.core.dtypes.generic import ( ABCDataFrame, ABCDatetimeIndex, @@ -224,7 +223,7 @@ def init_dict(data, index, columns, dtype=None): # no obvious "empty" int column if missing.any() and not is_integer_dtype(dtype): - if isinstance(dtype, CategoricalDtype): + if is_categorical_dtype(dtype): nan_dtype = dtype elif dtype is None or np.issubdtype(dtype, np.flexible): # GH#1783 From b76ade598f8e85df2570c350510e4b210442c851 Mon Sep 17 00:00:00 2001 From: Dustan Levenstein Date: Sun, 18 Aug 2019 13:31:04 -0500 Subject: [PATCH 6/6] Added unit test. --- pandas/tests/frame/test_dtypes.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandas/tests/frame/test_dtypes.py b/pandas/tests/frame/test_dtypes.py index 00be13b1c0e72..aa0bc21ec78ee 100644 --- a/pandas/tests/frame/test_dtypes.py +++ b/pandas/tests/frame/test_dtypes.py @@ -69,6 +69,10 @@ def test_empty_frame_dtypes_ftypes(self): norows_df = pd.DataFrame(columns=list("abc")) assert_series_equal(norows_df.dtypes, pd.Series(np.object, index=list("abc"))) + cat = pd.CategoricalDtype() + norows_cat_df = pd.DataFrame(columns=list("abc"), dtype=cat) + assert_series_equal(norows_cat_df.dtypes, pd.Series(cat, index=list("abc"))) + # GH 26705 - Assert .ftypes is deprecated with tm.assert_produces_warning(FutureWarning): assert_series_equal(