Skip to content

Commit 122f554

Browse files
ENH: Copy inputs in IntervalIndex constructor by default (GH#63388)
1 parent 923c949 commit 122f554

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

pandas/core/indexes/interval.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
)
3636
from pandas.util._exceptions import rewrite_exception
3737

38+
from pandas.core.dtypes.astype import astype_is_view
3839
from pandas.core.dtypes.cast import (
3940
find_common_type,
4041
infer_dtype_from_scalar,
@@ -61,6 +62,7 @@
6162
from pandas.core.dtypes.missing import is_valid_na_for_dtype
6263

6364
from pandas.core.algorithms import unique
65+
from pandas.core.arrays import ExtensionArray
6466
from pandas.core.arrays.datetimelike import validate_periods
6567
from pandas.core.arrays.interval import (
6668
IntervalArray,
@@ -169,8 +171,13 @@ class IntervalIndex(ExtensionIndex):
169171
neither.
170172
dtype : dtype or None, default None
171173
If None, dtype will be inferred.
172-
copy : bool, default False
173-
Copy the input data.
174+
copy : bool, default None
175+
Whether to copy input data, only relevant for array, Series, and Index
176+
inputs (for other input, e.g. a list, a new array is created anyway).
177+
Defaults to True for array input and False for Index/Series.
178+
Set to False to avoid copying array input at your own risk (if you
179+
know the input data won't be modified elsewhere).
180+
Set to True to force copying Series/Index input up front.
174181
name : object, optional
175182
Name to be stored in the index.
176183
verify_integrity : bool, default True
@@ -252,12 +259,19 @@ def __new__(
252259
data,
253260
closed: IntervalClosedType | None = None,
254261
dtype: Dtype | None = None,
255-
copy: bool = False,
262+
copy: bool | None = None,
256263
name: Hashable | None = None,
257264
verify_integrity: bool = True,
258265
) -> Self:
259266
name = maybe_extract_name(name, data, cls)
260267

268+
if isinstance(data, (ExtensionArray, np.ndarray)):
269+
# GH#63388
270+
if copy is not False:
271+
if dtype is None or astype_is_view(data.dtype, dtype):
272+
data = data.copy()
273+
copy = False
274+
261275
with rewrite_exception("IntervalArray", cls.__name__):
262276
array = IntervalArray(
263277
data,

0 commit comments

Comments
 (0)