Skip to content

Commit 7c2573c

Browse files
[Annika Rudolph]annika-rudolph
[Annika Rudolph]
authored andcommitted
add take function including frequency for Timedelta and Datetime Arrays
1 parent 78a2ef2 commit 7c2573c

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

pandas/core/arrays/datetimelike.py

+32-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import numpy as np
2222

23+
from pandas.core.algorithms import take
2324
from pandas._config.config import get_option
2425

2526
from pandas._libs import (
@@ -68,6 +69,7 @@
6869
SequenceIndexer,
6970
TimeAmbiguous,
7071
TimeNonexistent,
72+
TakeIndexer,
7173
npt,
7274
)
7375
from pandas.compat.numpy import function as nv
@@ -1797,7 +1799,7 @@ def strftime(self, date_format: str) -> npt.NDArray[np.object_]:
17971799
----------
17981800
freq : str or Offset
17991801
The frequency level to {op} the index to. Must be a fixed
1800-
frequency like 's' (second) not 'ME' (month end). See
1802+
frequency like 'S' (second) not 'ME' (month end). See
18011803
:ref:`frequency aliases <timeseries.offset_aliases>` for
18021804
a list of possible `freq` values.
18031805
ambiguous : 'infer', bool-ndarray, 'NaT', default 'raise'
@@ -1835,11 +1837,6 @@ def strftime(self, date_format: str) -> npt.NDArray[np.object_]:
18351837
------
18361838
ValueError if the `freq` cannot be converted.
18371839
1838-
See Also
1839-
--------
1840-
DatetimeIndex.floor : Perform floor operation on the data to the specified `freq`.
1841-
DatetimeIndex.snap : Snap time stamps to nearest occurring frequency.
1842-
18431840
Notes
18441841
-----
18451842
If the timestamps have a timezone, {op}ing will take place relative to the
@@ -2368,6 +2365,35 @@ def interpolate(
23682365
if not copy:
23692366
return self
23702367
return type(self)._simple_new(out_data, dtype=self.dtype)
2368+
2369+
def take(
2370+
self,
2371+
indices: TakeIndexer,
2372+
*,
2373+
allow_fill: bool = False,
2374+
fill_value: Any = None,
2375+
axis: AxisInt = 0,
2376+
) -> Self:
2377+
2378+
if allow_fill:
2379+
fill_value = self._validate_scalar(fill_value)
2380+
2381+
new_data = take(
2382+
self._ndarray,
2383+
indices,
2384+
allow_fill=allow_fill,
2385+
fill_value=fill_value,
2386+
axis=axis,
2387+
)
2388+
result = self._from_backing_data(new_data)
2389+
indices = np.asarray(indices, dtype=np.intp)
2390+
maybe_slice = lib.maybe_indices_to_slice(indices, len(self))
2391+
2392+
if isinstance(maybe_slice, slice):
2393+
freq = self._get_getitem_freq(maybe_slice)
2394+
result.freq = freq
2395+
2396+
return result
23712397

23722398
# --------------------------------------------------------------
23732399
# Unsorted

0 commit comments

Comments
 (0)