Skip to content

Commit 91512a8

Browse files
authored
REF: Implement core._algos (#32767)
1 parent e743ed7 commit 91512a8

File tree

4 files changed

+46
-40
lines changed

4 files changed

+46
-40
lines changed

pandas/core/array_algos/__init__.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""
2+
core.array_algos is for algorithms that operate on ndarray and ExtensionArray.
3+
These should:
4+
5+
- Assume that any Index, Series, or DataFrame objects have already been unwrapped.
6+
- Assume that any list arguments have already been cast to ndarray/EA.
7+
- Not depend on Index, Series, or DataFrame, nor import any of these.
8+
- May dispatch to ExtensionArray methods, but should not import from core.arrays.
9+
"""

pandas/core/array_algos/transforms.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
transforms.py is for shape-preserving functions.
3+
"""
4+
5+
import numpy as np
6+
7+
from pandas.core.dtypes.common import ensure_platform_int
8+
9+
10+
def shift(values: np.ndarray, periods: int, axis: int, fill_value) -> np.ndarray:
11+
new_values = values
12+
13+
# make sure array sent to np.roll is c_contiguous
14+
f_ordered = values.flags.f_contiguous
15+
if f_ordered:
16+
new_values = new_values.T
17+
axis = new_values.ndim - axis - 1
18+
19+
if np.prod(new_values.shape):
20+
new_values = np.roll(new_values, ensure_platform_int(periods), axis=axis)
21+
22+
axis_indexer = [slice(None)] * values.ndim
23+
if periods > 0:
24+
axis_indexer[axis] = slice(None, periods)
25+
else:
26+
axis_indexer[axis] = slice(periods, None)
27+
new_values[tuple(axis_indexer)] = fill_value
28+
29+
# restore original order
30+
if f_ordered:
31+
new_values = new_values.T
32+
33+
return new_values

pandas/core/arrays/datetimelike.py

+2-20
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
from pandas.core import missing, nanops, ops
4242
from pandas.core.algorithms import checked_add_with_arr, take, unique1d, value_counts
43+
from pandas.core.array_algos.transforms import shift
4344
from pandas.core.arrays.base import ExtensionArray, ExtensionOpsMixin
4445
import pandas.core.common as com
4546
from pandas.core.construction import array, extract_array
@@ -773,26 +774,7 @@ def shift(self, periods=1, fill_value=None, axis=0):
773774

774775
fill_value = self._unbox_scalar(fill_value)
775776

776-
new_values = self._data
777-
778-
# make sure array sent to np.roll is c_contiguous
779-
f_ordered = new_values.flags.f_contiguous
780-
if f_ordered:
781-
new_values = new_values.T
782-
axis = new_values.ndim - axis - 1
783-
784-
new_values = np.roll(new_values, periods, axis=axis)
785-
786-
axis_indexer = [slice(None)] * self.ndim
787-
if periods > 0:
788-
axis_indexer[axis] = slice(None, periods)
789-
else:
790-
axis_indexer[axis] = slice(periods, None)
791-
new_values[tuple(axis_indexer)] = fill_value
792-
793-
# restore original order
794-
if f_ordered:
795-
new_values = new_values.T
777+
new_values = shift(self._data, periods, axis, fill_value)
796778

797779
return type(self)._simple_new(new_values, dtype=self.dtype)
798780

pandas/core/internals/blocks.py

+2-20
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from pandas.core.dtypes.common import (
3030
_NS_DTYPE,
3131
_TD_DTYPE,
32-
ensure_platform_int,
3332
is_bool_dtype,
3433
is_categorical,
3534
is_categorical_dtype,
@@ -66,6 +65,7 @@
6665
)
6766

6867
import pandas.core.algorithms as algos
68+
from pandas.core.array_algos.transforms import shift
6969
from pandas.core.arrays import (
7070
Categorical,
7171
DatetimeArray,
@@ -1316,25 +1316,7 @@ def shift(self, periods, axis: int = 0, fill_value=None):
13161316
# that, handle boolean etc also
13171317
new_values, fill_value = maybe_upcast(self.values, fill_value)
13181318

1319-
# make sure array sent to np.roll is c_contiguous
1320-
f_ordered = new_values.flags.f_contiguous
1321-
if f_ordered:
1322-
new_values = new_values.T
1323-
axis = new_values.ndim - axis - 1
1324-
1325-
if np.prod(new_values.shape):
1326-
new_values = np.roll(new_values, ensure_platform_int(periods), axis=axis)
1327-
1328-
axis_indexer = [slice(None)] * self.ndim
1329-
if periods > 0:
1330-
axis_indexer[axis] = slice(None, periods)
1331-
else:
1332-
axis_indexer[axis] = slice(periods, None)
1333-
new_values[tuple(axis_indexer)] = fill_value
1334-
1335-
# restore original order
1336-
if f_ordered:
1337-
new_values = new_values.T
1319+
new_values = shift(new_values, periods, axis, fill_value)
13381320

13391321
return [self.make_block(new_values)]
13401322

0 commit comments

Comments
 (0)