|
7 | 7 | cast, |
8 | 8 | ) |
9 | 9 |
|
| 10 | +import numpy as np |
| 11 | + |
10 | 12 | from pandas._libs import ( |
11 | 13 | index as libindex, |
12 | 14 | lib, |
|
23 | 25 | is_scalar, |
24 | 26 | pandas_dtype, |
25 | 27 | ) |
| 28 | +from pandas.core.dtypes.astype import astype_is_view |
26 | 29 | from pandas.core.dtypes.dtypes import ArrowDtype |
27 | 30 | from pandas.core.dtypes.generic import ABCSeries |
28 | 31 |
|
| 32 | +from pandas.core.arrays import ExtensionArray |
29 | 33 | from pandas.core.arrays.timedeltas import TimedeltaArray |
30 | 34 | import pandas.core.common as com |
31 | 35 | from pandas.core.indexes.base import ( |
@@ -81,8 +85,13 @@ class TimedeltaIndex(DatetimeTimedeltaMixin): |
81 | 85 | dtype : numpy.dtype or str, default None |
82 | 86 | Valid ``numpy`` dtypes are ``timedelta64[ns]``, ``timedelta64[us]``, |
83 | 87 | ``timedelta64[ms]``, and ``timedelta64[s]``. |
84 | | - copy : bool |
85 | | - Make a copy of input array. |
| 88 | + copy : bool, default None |
| 89 | + Whether to copy input data, only relevant for array, Series, and Index |
| 90 | + inputs (for other input, e.g. a list, a new array is created anyway). |
| 91 | + Defaults to True for array input and False for Index/Series. |
| 92 | + Set to False to avoid copying array input at your own risk (if you |
| 93 | + know the input data won't be modified elsewhere). |
| 94 | + Set to True to force copying Series/Index input up front. |
86 | 95 | name : object |
87 | 96 | Name to be stored in the index. |
88 | 97 |
|
@@ -158,11 +167,18 @@ def __new__( |
158 | 167 | data=None, |
159 | 168 | freq=lib.no_default, |
160 | 169 | dtype=None, |
161 | | - copy: bool = False, |
| 170 | + copy: bool | None = None, |
162 | 171 | name=None, |
163 | 172 | ): |
164 | 173 | name = maybe_extract_name(name, data, cls) |
165 | 174 |
|
| 175 | + if isinstance(data, (ExtensionArray, np.ndarray)): |
| 176 | + # GH 63388 |
| 177 | + if copy is not False: |
| 178 | + if dtype is None or astype_is_view(data.dtype, dtype): |
| 179 | + data = data.copy() |
| 180 | + copy = False |
| 181 | + |
166 | 182 | if is_scalar(data): |
167 | 183 | cls._raise_scalar_data_error(data) |
168 | 184 |
|
|
0 commit comments