Skip to content

Commit c688a0f

Browse files
authored
BUG: Don't raise when constructing Series from ordered set (pandas-dev#36054)
1 parent cda2f54 commit c688a0f

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

doc/source/whatsnew/v1.1.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Bug fixes
3535
- Bug in :meth:`DataFrame.apply` with ``result_type="reduce"`` returning with incorrect index (:issue:`35683`)
3636
- Bug in :meth:`DateTimeIndex.format` and :meth:`PeriodIndex.format` with ``name=True`` setting the first item to ``"None"`` where it should be ``""`` (:issue:`35712`)
3737
- Bug in :meth:`Float64Index.__contains__` incorrectly raising ``TypeError`` instead of returning ``False`` (:issue:`35788`)
38+
- Bug in :class:`Series` constructor incorrectly raising a ``TypeError`` when passed an ordered set (:issue:`36044`)
3839
- Bug in :meth:`Series.dt.isocalendar` and :meth:`DatetimeIndex.isocalendar` that returned incorrect year for certain dates (:issue:`36032`)
3940
- 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`)
4041
- Bug in :meth:`DataFrame.corr` causing subsequent indexing lookups to be incorrect (:issue:`35882`)

pandas/core/construction.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,12 @@ def sanitize_array(
438438
subarr = subarr.copy()
439439
return subarr
440440

441-
elif isinstance(data, (list, tuple)) and len(data) > 0:
441+
elif isinstance(data, (list, tuple, abc.Set, abc.ValuesView)) and len(data) > 0:
442+
if isinstance(data, set):
443+
# Raise only for unordered sets, e.g., not for dict_keys
444+
raise TypeError("Set type is unordered")
445+
data = list(data)
446+
442447
if dtype is not None:
443448
subarr = _try_cast(data, dtype, copy, raise_cast_failure)
444449
else:
@@ -450,8 +455,6 @@ def sanitize_array(
450455
# GH#16804
451456
arr = np.arange(data.start, data.stop, data.step, dtype="int64")
452457
subarr = _try_cast(arr, dtype, copy, raise_cast_failure)
453-
elif isinstance(data, abc.Set):
454-
raise TypeError("Set type is unordered")
455458
elif lib.is_scalar(data) and index is not None and dtype is not None:
456459
data = maybe_cast_to_datetime(data, dtype)
457460
if not lib.is_scalar(data):

pandas/tests/series/test_constructors.py

+10
Original file line numberDiff line numberDiff line change
@@ -1464,3 +1464,13 @@ def test_constructor_sparse_datetime64(self, values):
14641464
arr = pd.arrays.SparseArray(values, dtype=dtype)
14651465
expected = pd.Series(arr)
14661466
tm.assert_series_equal(result, expected)
1467+
1468+
def test_construction_from_ordered_collection(self):
1469+
# https://github.com/pandas-dev/pandas/issues/36044
1470+
result = Series({"a": 1, "b": 2}.keys())
1471+
expected = Series(["a", "b"])
1472+
tm.assert_series_equal(result, expected)
1473+
1474+
result = Series({"a": 1, "b": 2}.values())
1475+
expected = Series([1, 2])
1476+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)