Skip to content

Commit 41d5a80

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

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
@@ -1787,7 +1789,7 @@ def strftime(self, date_format: str) -> npt.NDArray[np.object_]:
17871789
----------
17881790
freq : str or Offset
17891791
The frequency level to {op} the index to. Must be a fixed
1790-
frequency like 's' (second) not 'ME' (month end). See
1792+
frequency like 'S' (second) not 'ME' (month end). See
17911793
:ref:`frequency aliases <timeseries.offset_aliases>` for
17921794
a list of possible `freq` values.
17931795
ambiguous : 'infer', bool-ndarray, 'NaT', default 'raise'
@@ -1825,11 +1827,6 @@ def strftime(self, date_format: str) -> npt.NDArray[np.object_]:
18251827
------
18261828
ValueError if the `freq` cannot be converted.
18271829
1828-
See Also
1829-
--------
1830-
DatetimeIndex.floor : Perform floor operation on the data to the specified `freq`.
1831-
DatetimeIndex.snap : Snap time stamps to nearest occurring frequency.
1832-
18331830
Notes
18341831
-----
18351832
If the timestamps have a timezone, {op}ing will take place relative to the
@@ -2358,6 +2355,35 @@ def interpolate(
23582355
if not copy:
23592356
return self
23602357
return type(self)._simple_new(out_data, dtype=self.dtype)
2358+
2359+
def take(
2360+
self,
2361+
indices: TakeIndexer,
2362+
*,
2363+
allow_fill: bool = False,
2364+
fill_value: Any = None,
2365+
axis: AxisInt = 0,
2366+
) -> Self:
2367+
2368+
if allow_fill:
2369+
fill_value = self._validate_scalar(fill_value)
2370+
2371+
new_data = take(
2372+
self._ndarray,
2373+
indices,
2374+
allow_fill=allow_fill,
2375+
fill_value=fill_value,
2376+
axis=axis,
2377+
)
2378+
result = self._from_backing_data(new_data)
2379+
indices = np.asarray(indices, dtype=np.intp)
2380+
maybe_slice = lib.maybe_indices_to_slice(indices, len(self))
2381+
2382+
if isinstance(maybe_slice, slice):
2383+
freq = self._get_getitem_freq(maybe_slice)
2384+
result.freq = freq
2385+
2386+
return result
23612387

23622388
# --------------------------------------------------------------
23632389
# Unsorted

0 commit comments

Comments
 (0)