Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 15 additions & 20 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"""
from __future__ import annotations

from collections import abc
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -501,6 +500,16 @@ def sanitize_array(
if dtype is None:
dtype = data.dtype
data = lib.item_from_zerodim(data)
elif isinstance(data, range):
# GH#16804
data = np.arange(data.start, data.stop, data.step, dtype="int64")
copy = False

if not is_list_like(data):
if index is None:
raise ValueError("index must be specified when data is not list-like")
data = construct_1d_arraylike_from_scalar(data, len(index), dtype)
return data

# GH#846
if isinstance(data, np.ndarray):
Expand All @@ -525,14 +534,16 @@ def sanitize_array(
subarr = subarr.copy()
return subarr

elif isinstance(data, (list, tuple, abc.Set, abc.ValuesView)) and len(data) > 0:
# TODO: deque, array.array
else:
if isinstance(data, (set, frozenset)):
# Raise only for unordered sets, e.g., not for dict_keys
raise TypeError(f"'{type(data).__name__}' type is unordered")

# materialize e.g. generators, convert e.g. tuples, abc.ValueView
# TODO: non-standard array-likes we can convert to ndarray more efficiently?
data = list(data)

if dtype is not None:
if dtype is not None or len(data) == 0:
subarr = _try_cast(data, dtype, copy, raise_cast_failure)
else:
subarr = maybe_convert_platform(data)
Expand All @@ -541,22 +552,6 @@ def sanitize_array(
# "ExtensionArray")
subarr = maybe_cast_to_datetime(subarr, dtype) # type: ignore[assignment]

elif isinstance(data, range):
# GH#16804
arr = np.arange(data.start, data.stop, data.step, dtype="int64")
subarr = _try_cast(arr, dtype, copy, raise_cast_failure)

elif not is_list_like(data):
if index is None:
raise ValueError("index must be specified when data is not list-like")
subarr = construct_1d_arraylike_from_scalar(data, len(index), dtype)

else:
# realize e.g. generators
# TODO: non-standard array-likes we can convert to ndarray more efficiently?
data = list(data)
subarr = _try_cast(data, dtype, copy, raise_cast_failure)

subarr = _sanitize_ndim(subarr, data, dtype, index)

if not (
Expand Down
6 changes: 2 additions & 4 deletions pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,10 +554,8 @@ def convert(v):

else:

# drop subclass info, do not copy data
values = np.asarray(values)
if copy:
values = values.copy()
# drop subclass info
values = np.array(values, copy=copy)

if values.ndim == 1:
values = values.reshape((values.shape[0], 1))
Expand Down