From 364c1ae98e91ae70b1df7b8e69c0b8886f5930a0 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Tue, 1 Sep 2020 17:08:35 -0500 Subject: [PATCH 1/5] BUG: Don't raise when constructing Series from ordered set --- doc/source/whatsnew/v1.1.2.rst | 2 ++ pandas/core/construction.py | 8 +++++--- pandas/tests/series/test_constructors.py | 6 ++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v1.1.2.rst b/doc/source/whatsnew/v1.1.2.rst index 9b1ad658d4666..fb59e34e5b358 100644 --- a/doc/source/whatsnew/v1.1.2.rst +++ b/doc/source/whatsnew/v1.1.2.rst @@ -32,6 +32,8 @@ Bug fixes - Bug in :meth:`DataFrame.apply` with ``result_type="reduce"`` returning with incorrect index (:issue:`35683`) - Bug in :meth:`DateTimeIndex.format` and :meth:`PeriodIndex.format` with ``name=True`` setting the first item to ``"None"`` where it should bw ``""`` (:issue:`35712`) - Bug in :meth:`Float64Index.__contains__` incorrectly raising ``TypeError`` instead of returning ``False`` (:issue:`35788`) +- Bug in :class:`Series` constructor incorrectly raising a ``TypeError`` when passed an ordered set (:issue:`36044`) +- .. --------------------------------------------------------------------------- diff --git a/pandas/core/construction.py b/pandas/core/construction.py index f145e76046bee..714ca05dd1f85 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -437,7 +437,11 @@ def sanitize_array( subarr = subarr.copy() return subarr - elif isinstance(data, (list, tuple)) and len(data) > 0: + elif isinstance(data, (list, tuple, abc.Set)) and len(data) > 0: + if isinstance(data, set): + raise TypeError("Set type is unordered") + data = list(data) + if dtype is not None: subarr = _try_cast(data, dtype, copy, raise_cast_failure) else: @@ -449,8 +453,6 @@ def sanitize_array( # GH#16804 arr = np.arange(data.start, data.stop, data.step, dtype="int64") subarr = _try_cast(arr, dtype, copy, raise_cast_failure) - elif isinstance(data, abc.Set): - raise TypeError("Set type is unordered") elif lib.is_scalar(data) and index is not None and dtype is not None: data = maybe_cast_to_datetime(data, dtype) if not lib.is_scalar(data): diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index bcf7039ec9039..8be6819f13ff5 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -1464,3 +1464,9 @@ def test_constructor_sparse_datetime64(self, values): arr = pd.arrays.SparseArray(values, dtype=dtype) expected = pd.Series(arr) tm.assert_series_equal(result, expected) + + def test_construction_from_ordered_set(self): + # https://github.com/pandas-dev/pandas/issues/36044 + result = Series({"a": 1, "b": 2}.keys()) + expected = Series(["a", "b"]) + tm.assert_series_equal(result, expected) From ede9d835a5eb445cf8be501462a267bc8e80b511 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Tue, 1 Sep 2020 18:03:39 -0500 Subject: [PATCH 2/5] Comment --- pandas/core/construction.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 714ca05dd1f85..bc1dc5fef2674 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -439,6 +439,7 @@ def sanitize_array( elif isinstance(data, (list, tuple, abc.Set)) and len(data) > 0: if isinstance(data, set): + # Raise only for unordered sets, e.g., not for dict_keys raise TypeError("Set type is unordered") data = list(data) From d15f9402b205a89963ab7513e24afcd927a10769 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Tue, 1 Sep 2020 19:56:09 -0500 Subject: [PATCH 3/5] dict_values --- pandas/core/construction.py | 2 +- pandas/tests/series/test_constructors.py | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index bc1dc5fef2674..e17294465a3b3 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -437,7 +437,7 @@ def sanitize_array( subarr = subarr.copy() return subarr - elif isinstance(data, (list, tuple, abc.Set)) and len(data) > 0: + elif isinstance(data, (list, tuple, abc.Set, abc.ValuesView)) and len(data) > 0: if isinstance(data, set): # Raise only for unordered sets, e.g., not for dict_keys raise TypeError("Set type is unordered") diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 8be6819f13ff5..ce078059479b4 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -1465,8 +1465,12 @@ def test_constructor_sparse_datetime64(self, values): expected = pd.Series(arr) tm.assert_series_equal(result, expected) - def test_construction_from_ordered_set(self): + def test_construction_from_ordered_collection(self): # https://github.com/pandas-dev/pandas/issues/36044 result = Series({"a": 1, "b": 2}.keys()) expected = Series(["a", "b"]) tm.assert_series_equal(result, expected) + + result = Series({"a": 1, "b": 2}.values()) + expected = Series([1, 2]) + tm.assert_series_equal(result, expected) From 6fbc019e5048bab024e19a74579e50c335456f57 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Thu, 3 Sep 2020 00:33:02 -0400 Subject: [PATCH 4/5] fixup --- doc/source/whatsnew/v1.1.2.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.2.rst b/doc/source/whatsnew/v1.1.2.rst index 5c69b98a34b93..6bdbb912f455c 100644 --- a/doc/source/whatsnew/v1.1.2.rst +++ b/doc/source/whatsnew/v1.1.2.rst @@ -42,7 +42,6 @@ Bug fixes Other ~~~~~ - :meth:`factorize` now supports ``na_sentinel=None`` to include NaN in the uniques of the values and remove ``dropna`` keyword which was unintentionally exposed to public facing API in 1.1 version from :meth:`factorize`(:issue:`35667`) ->>>>>>> upstream/master .. --------------------------------------------------------------------------- From 378d41cd31dc1fa37a8a6f51983a9c10cbd43d9f Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Fri, 4 Sep 2020 11:51:38 -0400 Subject: [PATCH 5/5] Fix --- doc/source/whatsnew/v1.1.2.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.2.rst b/doc/source/whatsnew/v1.1.2.rst index 94be9c7236069..dc864cec1daf3 100644 --- a/doc/source/whatsnew/v1.1.2.rst +++ b/doc/source/whatsnew/v1.1.2.rst @@ -34,7 +34,6 @@ Bug fixes - Bug in :meth:`DateTimeIndex.format` and :meth:`PeriodIndex.format` with ``name=True`` setting the first item to ``"None"`` where it should be ``""`` (:issue:`35712`) - Bug in :meth:`Float64Index.__contains__` incorrectly raising ``TypeError`` instead of returning ``False`` (:issue:`35788`) - Bug in :class:`Series` constructor incorrectly raising a ``TypeError`` when passed an ordered set (:issue:`36044`) -- Bug in :class:`DataFrame` indexing returning an incorrect :class:`Series` in some cases when the series has been altered and a cache not invalidated (:issue:`36051`) - Bug in :class:`DataFrame` indexing returning an incorrect :class:`Series` in some cases when the series has been altered and a cache not invalidated (:issue:`33675`) .. ---------------------------------------------------------------------------