Skip to content

Commit e313e14

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

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
@@ -19,6 +19,7 @@
1919

2020
import numpy as np
2121

22+
from pandas.core.algorithms import take
2223
from pandas._config.config import get_option
2324

2425
from pandas._libs import (
@@ -67,6 +68,7 @@
6768
SequenceIndexer,
6869
TimeAmbiguous,
6970
TimeNonexistent,
71+
TakeIndexer,
7072
npt,
7173
)
7274
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)