Skip to content

Commit 6b9975d

Browse files
Revert "Shift to adding frequency to shallow copy of DatetimeIndex instead of modifying take function"
This reverts commit 02c2f95.
1 parent 85aecbf commit 6b9975d

File tree

6 files changed

+30
-29
lines changed

6 files changed

+30
-29
lines changed

pandas/core/arrays/datetimelike.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
ScalarIndexer,
6767
Self,
6868
SequenceIndexer,
69+
TakeIndexer,
6970
TimeAmbiguous,
7071
TimeNonexistent,
7172
npt,
@@ -2364,6 +2365,27 @@ def interpolate(
23642365
return self
23652366
return type(self)._simple_new(out_data, dtype=self.dtype)
23662367

2368+
def take(
2369+
self,
2370+
indices: TakeIndexer,
2371+
*,
2372+
allow_fill: bool = False,
2373+
fill_value: Any = None,
2374+
axis: AxisInt = 0,
2375+
) -> Self:
2376+
result = super().take(
2377+
indices=indices, allow_fill=allow_fill, fill_value=fill_value, axis=axis
2378+
)
2379+
2380+
indices = np.asarray(indices, dtype=np.intp)
2381+
maybe_slice = lib.maybe_indices_to_slice(indices, len(self))
2382+
2383+
if isinstance(maybe_slice, slice):
2384+
freq = self._get_getitem_freq(maybe_slice)
2385+
result.freq = freq
2386+
2387+
return result
2388+
23672389
# --------------------------------------------------------------
23682390
# Unsorted
23692391

pandas/core/indexes/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ def _format_duplicate_message(self) -> DataFrame:
748748
# --------------------------------------------------------------------
749749
# Index Internals Methods
750750

751-
def _shallow_copy(self, values, name: Hashable = no_default, **kwargs) -> Self:
751+
def _shallow_copy(self, values, name: Hashable = no_default) -> Self:
752752
"""
753753
Create a new Index with the same class as the caller, don't copy the
754754
data, use the same object attributes with passed in attributes taking

pandas/core/indexes/datetimelike.py

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
Timedelta,
2323
lib,
2424
)
25-
from pandas._libs.lib import no_default
2625
from pandas._libs.tslibs import (
2726
BaseOffset,
2827
Resolution,
@@ -69,10 +68,7 @@
6968
from pandas.core.tools.timedeltas import to_timedelta
7069

7170
if TYPE_CHECKING:
72-
from collections.abc import (
73-
Hashable,
74-
Sequence,
75-
)
71+
from collections.abc import Sequence
7672
from datetime import datetime
7773

7874
from pandas._typing import (
@@ -842,20 +838,3 @@ def take(
842838
freq = self._data._get_getitem_freq(maybe_slice)
843839
result._data._freq = freq
844840
return result
845-
846-
@doc(Index._shallow_copy)
847-
def _shallow_copy( # type: ignore[override]
848-
self,
849-
values,
850-
name: Hashable = no_default,
851-
level_codes=no_default,
852-
) -> Self:
853-
name = self._name if name is no_default else name
854-
result = self._simple_new(values, name=name, refs=self._references)
855-
if level_codes is not no_default:
856-
indices = np.asarray(level_codes, dtype=np.intp)
857-
maybe_slice = lib.maybe_indices_to_slice(indices, len(values))
858-
if isinstance(maybe_slice, slice):
859-
freq = self._data._get_getitem_freq(maybe_slice)
860-
result._data._freq = freq
861-
return result

pandas/core/indexes/multi.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,9 +1244,7 @@ def _constructor(self) -> Callable[..., MultiIndex]: # type: ignore[override]
12441244
return type(self).from_tuples
12451245

12461246
@doc(Index._shallow_copy)
1247-
def _shallow_copy(
1248-
self, values: np.ndarray, name=lib.no_default, **kwargs
1249-
) -> MultiIndex:
1247+
def _shallow_copy(self, values: np.ndarray, name=lib.no_default) -> MultiIndex:
12501248
names = name if name is not lib.no_default else self.names
12511249

12521250
return type(self).from_tuples(values, sortorder=None, names=names)
@@ -1716,7 +1714,7 @@ def _get_level_values(self, level: int, unique: bool = False) -> Index:
17161714
if unique:
17171715
level_codes = algos.unique(level_codes)
17181716
filled = algos.take_nd(lev._values, level_codes, fill_value=lev._na_value)
1719-
return lev._shallow_copy(filled, name=name, level_codes=level_codes)
1717+
return lev._shallow_copy(filled, name=name)
17201718

17211719
# error: Signature of "get_level_values" incompatible with supertype "Index"
17221720
def get_level_values(self, level) -> Index: # type: ignore[override]

pandas/core/indexes/range.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ def __iter__(self) -> Iterator[int]:
472472
yield from self._range
473473

474474
@doc(Index._shallow_copy)
475-
def _shallow_copy(self, values, name: Hashable = no_default, **kwargs):
475+
def _shallow_copy(self, values, name: Hashable = no_default):
476476
name = self._name if name is no_default else name
477477

478478
if values.dtype.kind == "f":

pandas/tests/indexes/multi/test_get_level_values.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ def test_values_loses_freq_of_underlying_index():
126126

127127
def test_get_level_values_gets_frequency_correctly():
128128
# GH#57949 GH#58327
129-
datetime_index = date_range(start=pd.to_datetime("1/1/2018"), periods=4, freq="YS")
129+
datetime_index = pd.date_range(
130+
start=pd.to_datetime("1/1/2018"), periods=4, freq="YS"
131+
)
130132
other_index = ["A"]
131133
multi_index = MultiIndex.from_product([datetime_index, other_index])
132134

0 commit comments

Comments
 (0)