Skip to content

Commit ac13a09

Browse files
annika-rudolph[Annika Rudolph]
and
[Annika Rudolph]
authored
BUG: Add frequency to DatetimeArray/TimedeltaArray take (#58382)
* add take function including frequency for Timedelta and Datetime Arrays * add test for frequency of DatetimeIndex in MultiIndex * use super() in take function * add description to whatsnew and revert unwanted changes in datetimearray docstring make pre-commit happy * switch .freq to ._freq --------- Co-authored-by: [Annika Rudolph] <[[email protected]]>
1 parent 9e2bab1 commit ac13a09

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ MultiIndex
556556
- :func:`DataFrame.loc` with ``axis=0`` and :class:`MultiIndex` when setting a value adds extra columns (:issue:`58116`)
557557
- :meth:`DataFrame.melt` would not accept multiple names in ``var_name`` when the columns were a :class:`MultiIndex` (:issue:`58033`)
558558
- :meth:`MultiIndex.insert` would not insert NA value correctly at unified location of index -1 (:issue:`59003`)
559+
- :func:`MultiIndex.get_level_values` accessing a :class:`DatetimeIndex` does not carry the frequency attribute along (:issue:`58327`, :issue:`57949`)
559560
-
560561

561562
I/O

pandas/core/arrays/datetimelike.py

+22
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
ScalarIndexer,
6666
Self,
6767
SequenceIndexer,
68+
TakeIndexer,
6869
TimeAmbiguous,
6970
TimeNonexistent,
7071
npt,
@@ -2340,6 +2341,27 @@ def interpolate(
23402341
return self
23412342
return type(self)._simple_new(out_data, dtype=self.dtype)
23422343

2344+
def take(
2345+
self,
2346+
indices: TakeIndexer,
2347+
*,
2348+
allow_fill: bool = False,
2349+
fill_value: Any = None,
2350+
axis: AxisInt = 0,
2351+
) -> Self:
2352+
result = super().take(
2353+
indices=indices, allow_fill=allow_fill, fill_value=fill_value, axis=axis
2354+
)
2355+
2356+
indices = np.asarray(indices, dtype=np.intp)
2357+
maybe_slice = lib.maybe_indices_to_slice(indices, len(self))
2358+
2359+
if isinstance(maybe_slice, slice):
2360+
freq = self._get_getitem_freq(maybe_slice)
2361+
result._freq = freq
2362+
2363+
return result
2364+
23432365
# --------------------------------------------------------------
23442366
# Unsorted
23452367

pandas/tests/indexes/multi/test_get_level_values.py

+9
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,12 @@ def test_values_loses_freq_of_underlying_index():
122122
midx.values
123123
assert idx.freq is not None
124124
tm.assert_index_equal(idx, expected)
125+
126+
127+
def test_get_level_values_gets_frequency_correctly():
128+
# GH#57949 GH#58327
129+
datetime_index = date_range(start=pd.to_datetime("1/1/2018"), periods=4, freq="YS")
130+
other_index = ["A"]
131+
multi_index = MultiIndex.from_product([datetime_index, other_index])
132+
133+
assert multi_index.get_level_values(0).freq == datetime_index.freq

0 commit comments

Comments
 (0)