Skip to content

Commit b3d5feb

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

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
@@ -1768,7 +1770,7 @@ def strftime(self, date_format: str) -> npt.NDArray[np.object_]:
17681770
----------
17691771
freq : str or Offset
17701772
The frequency level to {op} the index to. Must be a fixed
1771-
frequency like 's' (second) not 'ME' (month end). See
1773+
frequency like 'S' (second) not 'ME' (month end). See
17721774
:ref:`frequency aliases <timeseries.offset_aliases>` for
17731775
a list of possible `freq` values.
17741776
ambiguous : 'infer', bool-ndarray, 'NaT', default 'raise'
@@ -1806,11 +1808,6 @@ def strftime(self, date_format: str) -> npt.NDArray[np.object_]:
18061808
------
18071809
ValueError if the `freq` cannot be converted.
18081810
1809-
See Also
1810-
--------
1811-
DatetimeIndex.floor : Perform floor operation on the data to the specified `freq`.
1812-
DatetimeIndex.snap : Snap time stamps to nearest occurring frequency.
1813-
18141811
Notes
18151812
-----
18161813
If the timestamps have a timezone, {op}ing will take place relative to the
@@ -2339,6 +2336,35 @@ def interpolate(
23392336
if not copy:
23402337
return self
23412338
return type(self)._simple_new(out_data, dtype=self.dtype)
2339+
2340+
def take(
2341+
self,
2342+
indices: TakeIndexer,
2343+
*,
2344+
allow_fill: bool = False,
2345+
fill_value: Any = None,
2346+
axis: AxisInt = 0,
2347+
) -> Self:
2348+
2349+
if allow_fill:
2350+
fill_value = self._validate_scalar(fill_value)
2351+
2352+
new_data = take(
2353+
self._ndarray,
2354+
indices,
2355+
allow_fill=allow_fill,
2356+
fill_value=fill_value,
2357+
axis=axis,
2358+
)
2359+
result = self._from_backing_data(new_data)
2360+
indices = np.asarray(indices, dtype=np.intp)
2361+
maybe_slice = lib.maybe_indices_to_slice(indices, len(self))
2362+
2363+
if isinstance(maybe_slice, slice):
2364+
freq = self._get_getitem_freq(maybe_slice)
2365+
result.freq = freq
2366+
2367+
return result
23422368

23432369
# --------------------------------------------------------------
23442370
# Unsorted

0 commit comments

Comments
 (0)