Skip to content

Commit 65d8012

Browse files
REF: Use _maybe_copy_input in Index subclasses (GH#63388)
Updates DatetimeIndex, TimedeltaIndex, PeriodIndex, and IntervalIndex to use the shared validation logic, reducing code duplication.
1 parent e689f7b commit 65d8012

File tree

4 files changed

+8
-48
lines changed

4 files changed

+8
-48
lines changed

pandas/core/indexes/datetimes.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@
3737
)
3838
from pandas.util._exceptions import find_stack_level
3939

40-
from pandas.core.dtypes.astype import astype_is_view
4140
from pandas.core.dtypes.common import (
4241
is_scalar,
43-
pandas_dtype,
4442
)
4543
from pandas.core.dtypes.dtypes import (
4644
ArrowDtype,
@@ -49,7 +47,6 @@
4947
from pandas.core.dtypes.generic import ABCSeries
5048
from pandas.core.dtypes.missing import is_valid_na_for_dtype
5149

52-
from pandas.core.arrays import ExtensionArray
5350
from pandas.core.arrays.datetimes import (
5451
DatetimeArray,
5552
tz_to_dtype,
@@ -689,15 +686,8 @@ def __new__(
689686

690687
name = maybe_extract_name(name, data, cls)
691688

692-
if isinstance(data, (ExtensionArray, np.ndarray)):
693-
# GH 63388
694-
if copy is not False:
695-
if dtype is None or astype_is_view(
696-
data.dtype,
697-
pandas_dtype(dtype),
698-
):
699-
data = data.copy()
700-
copy = False
689+
# GH#63388
690+
data, copy = cls._maybe_copy_input(data, copy, dtype)
701691

702692
if (
703693
isinstance(data, DatetimeArray)

pandas/core/indexes/interval.py

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

38-
from pandas.core.dtypes.astype import astype_is_view
3938
from pandas.core.dtypes.cast import (
4039
find_common_type,
4140
infer_dtype_from_scalar,
@@ -62,7 +61,6 @@
6261
from pandas.core.dtypes.missing import is_valid_na_for_dtype
6362

6463
from pandas.core.algorithms import unique
65-
from pandas.core.arrays import ExtensionArray
6664
from pandas.core.arrays.datetimelike import validate_periods
6765
from pandas.core.arrays.interval import (
6866
IntervalArray,
@@ -265,15 +263,8 @@ def __new__(
265263
) -> Self:
266264
name = maybe_extract_name(name, data, cls)
267265

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(
272-
data.dtype,
273-
pandas_dtype(dtype),
274-
):
275-
data = data.copy()
276-
copy = False
266+
# GH#63388
267+
data, copy = cls._maybe_copy_input(data, copy, dtype)
277268

278269
with rewrite_exception("IntervalArray", cls.__name__):
279270
array = IntervalArray(

pandas/core/indexes/period.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,13 @@
2727
set_module,
2828
)
2929

30-
from pandas.core.dtypes.astype import astype_is_view
3130
from pandas.core.dtypes.common import (
3231
is_integer,
33-
pandas_dtype,
3432
)
3533
from pandas.core.dtypes.dtypes import PeriodDtype
3634
from pandas.core.dtypes.generic import ABCSeries
3735
from pandas.core.dtypes.missing import is_valid_na_for_dtype
3836

39-
from pandas.core.arrays import ExtensionArray
4037
from pandas.core.arrays.period import (
4138
PeriodArray,
4239
period_array,
@@ -241,15 +238,8 @@ def __new__(
241238

242239
freq = validate_dtype_freq(dtype, freq)
243240

244-
if isinstance(data, (ExtensionArray, np.ndarray)):
245-
# GH 63388
246-
if copy is not False:
247-
if dtype is None or astype_is_view(
248-
data.dtype,
249-
pandas_dtype(dtype),
250-
):
251-
data = data.copy()
252-
copy = False
241+
# GH#63388
242+
data, copy = cls._maybe_copy_input(data, copy, dtype)
253243

254244
# PeriodIndex allow PeriodIndex(period_index, freq=different)
255245
# Let's not encourage that kind of behavior in PeriodArray.

pandas/core/indexes/timedeltas.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
cast,
88
)
99

10-
import numpy as np
11-
1210
from pandas._libs import (
1311
index as libindex,
1412
lib,
@@ -21,15 +19,13 @@
2119
from pandas._libs.tslibs.dtypes import abbrev_to_npy_unit
2220
from pandas.util._decorators import set_module
2321

24-
from pandas.core.dtypes.astype import astype_is_view
2522
from pandas.core.dtypes.common import (
2623
is_scalar,
2724
pandas_dtype,
2825
)
2926
from pandas.core.dtypes.dtypes import ArrowDtype
3027
from pandas.core.dtypes.generic import ABCSeries
3128

32-
from pandas.core.arrays import ExtensionArray
3329
from pandas.core.arrays.timedeltas import TimedeltaArray
3430
import pandas.core.common as com
3531
from pandas.core.indexes.base import (
@@ -172,15 +168,8 @@ def __new__(
172168
):
173169
name = maybe_extract_name(name, data, cls)
174170

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(
179-
data.dtype,
180-
pandas_dtype(dtype),
181-
):
182-
data = data.copy()
183-
copy = False
171+
# GH#63388
172+
data, copy = cls._maybe_copy_input(data, copy, dtype)
184173

185174
if is_scalar(data):
186175
cls._raise_scalar_data_error(data)

0 commit comments

Comments
 (0)