Skip to content

Commit a61f1ad

Browse files
Backport PR #36054: BUG: Don't raise when constructing Series from ordered set (#36162)
Co-authored-by: Daniel Saxton <[email protected]>
1 parent 169a692 commit a61f1ad

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

pandas/core/construction.py

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

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