Skip to content

Commit 7b4fd2c

Browse files
Shift to adding frequency to shallow copy of DatetimeIndex instead of modifying take function
1 parent 1061565 commit 7b4fd2c

File tree

6 files changed

+29
-30
lines changed

6 files changed

+29
-30
lines changed

pandas/core/arrays/datetimelike.py

-22
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
ScalarIndexer,
6767
Self,
6868
SequenceIndexer,
69-
TakeIndexer,
7069
TimeAmbiguous,
7170
TimeNonexistent,
7271
npt,
@@ -2360,27 +2359,6 @@ def interpolate(
23602359
return self
23612360
return type(self)._simple_new(out_data, dtype=self.dtype)
23622361

2363-
def take(
2364-
self,
2365-
indices: TakeIndexer,
2366-
*,
2367-
allow_fill: bool = False,
2368-
fill_value: Any = None,
2369-
axis: AxisInt = 0,
2370-
) -> Self:
2371-
result = super().take(
2372-
indices=indices, allow_fill=allow_fill, fill_value=fill_value, axis=axis
2373-
)
2374-
2375-
indices = np.asarray(indices, dtype=np.intp)
2376-
maybe_slice = lib.maybe_indices_to_slice(indices, len(self))
2377-
2378-
if isinstance(maybe_slice, slice):
2379-
freq = self._get_getitem_freq(maybe_slice)
2380-
result.freq = freq
2381-
2382-
return result
2383-
23842362
# --------------------------------------------------------------
23852363
# Unsorted
23862364

pandas/core/indexes/base.py

+1-1
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) -> Self:
751+
def _shallow_copy(self, values, name: Hashable = no_default, **kwargs) -> 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

+22-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
Timedelta,
2323
lib,
2424
)
25+
from pandas._libs.lib import no_default
2526
from pandas._libs.tslibs import (
2627
BaseOffset,
2728
Resolution,
@@ -68,7 +69,10 @@
6869
from pandas.core.tools.timedeltas import to_timedelta
6970

7071
if TYPE_CHECKING:
71-
from collections.abc import Sequence
72+
from collections.abc import (
73+
Hashable,
74+
Sequence,
75+
)
7276
from datetime import datetime
7377

7478
from pandas._typing import (
@@ -838,3 +842,20 @@ def take(
838842
freq = self._data._get_getitem_freq(maybe_slice)
839843
result._data._freq = freq
840844
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

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

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

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

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

pandas/core/indexes/range.py

+1-1
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):
475+
def _shallow_copy(self, values, name: Hashable = no_default, **kwargs):
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

+1-3
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,7 @@ 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 = pd.date_range(
130-
start=pd.to_datetime("1/1/2018"), periods=4, freq="YS"
131-
)
129+
datetime_index = date_range(start=pd.to_datetime("1/1/2018"), periods=4, freq="YS")
132130
other_index = ["A"]
133131
multi_index = MultiIndex.from_product([datetime_index, other_index])
134132

0 commit comments

Comments
 (0)