Skip to content

BUG: Don't raise when constructing Series from ordered set #36054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Sep 5, 2020
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.1.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
-

.. ---------------------------------------------------------------------------

Expand Down
8 changes: 5 additions & 3 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)