Skip to content

Commit 943d191

Browse files
[Annika Rudolph]annika-rudolph
[Annika Rudolph]
authored andcommitted
add take function including frequency for Timedelta and Datetime Arrays
1 parent 6320c8b commit 943d191

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

pandas/core/arrays/datetimelike.py

Lines changed: 32 additions & 6 deletions
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
@@ -1792,7 +1794,7 @@ def strftime(self, date_format: str) -> npt.NDArray[np.object_]:
17921794
----------
17931795
freq : str or Offset
17941796
The frequency level to {op} the index to. Must be a fixed
1795-
frequency like 's' (second) not 'ME' (month end). See
1797+
frequency like 'S' (second) not 'ME' (month end). See
17961798
:ref:`frequency aliases <timeseries.offset_aliases>` for
17971799
a list of possible `freq` values.
17981800
ambiguous : 'infer', bool-ndarray, 'NaT', default 'raise'
@@ -1830,11 +1832,6 @@ def strftime(self, date_format: str) -> npt.NDArray[np.object_]:
18301832
------
18311833
ValueError if the `freq` cannot be converted.
18321834
1833-
See Also
1834-
--------
1835-
DatetimeIndex.floor : Perform floor operation on the data to the specified `freq`.
1836-
DatetimeIndex.snap : Snap time stamps to nearest occurring frequency.
1837-
18381835
Notes
18391836
-----
18401837
If the timestamps have a timezone, {op}ing will take place relative to the
@@ -2363,6 +2360,35 @@ def interpolate(
23632360
if not copy:
23642361
return self
23652362
return type(self)._simple_new(out_data, dtype=self.dtype)
2363+
2364+
def take(
2365+
self,
2366+
indices: TakeIndexer,
2367+
*,
2368+
allow_fill: bool = False,
2369+
fill_value: Any = None,
2370+
axis: AxisInt = 0,
2371+
) -> Self:
2372+
2373+
if allow_fill:
2374+
fill_value = self._validate_scalar(fill_value)
2375+
2376+
new_data = take(
2377+
self._ndarray,
2378+
indices,
2379+
allow_fill=allow_fill,
2380+
fill_value=fill_value,
2381+
axis=axis,
2382+
)
2383+
result = self._from_backing_data(new_data)
2384+
indices = np.asarray(indices, dtype=np.intp)
2385+
maybe_slice = lib.maybe_indices_to_slice(indices, len(self))
2386+
2387+
if isinstance(maybe_slice, slice):
2388+
freq = self._get_getitem_freq(maybe_slice)
2389+
result.freq = freq
2390+
2391+
return result
23662392

23672393
# --------------------------------------------------------------
23682394
# Unsorted

0 commit comments

Comments
 (0)