|
35 | 35 | ) |
36 | 36 | from pandas.util._exceptions import rewrite_exception |
37 | 37 |
|
| 38 | +from pandas.core.dtypes.astype import astype_is_view |
38 | 39 | from pandas.core.dtypes.cast import ( |
39 | 40 | find_common_type, |
40 | 41 | infer_dtype_from_scalar, |
|
61 | 62 | from pandas.core.dtypes.missing import is_valid_na_for_dtype |
62 | 63 |
|
63 | 64 | from pandas.core.algorithms import unique |
| 65 | +from pandas.core.arrays import ExtensionArray |
64 | 66 | from pandas.core.arrays.datetimelike import validate_periods |
65 | 67 | from pandas.core.arrays.interval import ( |
66 | 68 | IntervalArray, |
@@ -169,8 +171,13 @@ class IntervalIndex(ExtensionIndex): |
169 | 171 | neither. |
170 | 172 | dtype : dtype or None, default None |
171 | 173 | 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. |
174 | 181 | name : object, optional |
175 | 182 | Name to be stored in the index. |
176 | 183 | verify_integrity : bool, default True |
@@ -252,12 +259,19 @@ def __new__( |
252 | 259 | data, |
253 | 260 | closed: IntervalClosedType | None = None, |
254 | 261 | dtype: Dtype | None = None, |
255 | | - copy: bool = False, |
| 262 | + copy: bool | None = None, |
256 | 263 | name: Hashable | None = None, |
257 | 264 | verify_integrity: bool = True, |
258 | 265 | ) -> Self: |
259 | 266 | name = maybe_extract_name(name, data, cls) |
260 | 267 |
|
| 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 | + |
261 | 275 | with rewrite_exception("IntervalArray", cls.__name__): |
262 | 276 | array = IntervalArray( |
263 | 277 | data, |
|
0 commit comments