Skip to content

Commit 5f74e35

Browse files
jbrockmendelim-vinicius
authored and
im-vinicius
committed
REF: de-duplicate Block.diff, remove axis kwd (pandas-dev#53807)
* REF: remove axis keyword from internal diff methods * De-duplicate diff
1 parent 781de08 commit 5f74e35

File tree

5 files changed

+14
-45
lines changed

5 files changed

+14
-45
lines changed

pandas/core/algorithms.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1441,8 +1441,8 @@ def diff(arr, n: int, axis: AxisInt = 0):
14411441
arr = arr.to_numpy()
14421442
dtype = arr.dtype
14431443

1444-
if not isinstance(dtype, np.dtype):
1445-
# i.e ExtensionDtype
1444+
if not isinstance(arr, np.ndarray):
1445+
# i.e ExtensionArray
14461446
if hasattr(arr, f"__{op.__name__}__"):
14471447
if axis != 0:
14481448
raise ValueError(f"cannot diff {type(arr).__name__} on axis={axis}")

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9479,7 +9479,7 @@ def diff(self, periods: int = 1, axis: Axis = 0) -> DataFrame:
94799479
# With periods=0 this is equivalent to a diff with axis=0
94809480
axis = 0
94819481

9482-
new_data = self._mgr.diff(n=periods, axis=axis)
9482+
new_data = self._mgr.diff(n=periods)
94839483
return self._constructor(new_data).__finalize__(self, "diff")
94849484

94859485
# ----------------------------------------------------------------------

pandas/core/internals/array_manager.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,9 @@ def putmask(self, mask, new, align: bool = True) -> Self:
353353
new=new,
354354
)
355355

356-
def diff(self, n: int, axis: AxisInt) -> Self:
357-
assert self.ndim == 2 and axis == 0 # caller ensures
358-
return self.apply(algos.diff, n=n, axis=axis)
356+
def diff(self, n: int) -> Self:
357+
assert self.ndim == 2 # caller ensures
358+
return self.apply(algos.diff, n=n)
359359

360360
def interpolate(self, **kwargs) -> Self:
361361
return self.apply_with_block("interpolate", swap_axis=False, **kwargs)

pandas/core/internals/blocks.py

+5-35
Original file line numberDiff line numberDiff line change
@@ -1426,10 +1426,12 @@ def interpolate(
14261426
nb = self.make_block_same_class(data, refs=refs)
14271427
return nb._maybe_downcast([nb], downcast, using_cow)
14281428

1429-
def diff(self, n: int, axis: AxisInt = 1) -> list[Block]:
1429+
@final
1430+
def diff(self, n: int) -> list[Block]:
14301431
"""return block for the diff of the values"""
1431-
# only reached with ndim == 2 and axis == 1
1432-
new_values = algos.diff(self.values, n, axis=axis)
1432+
# only reached with ndim == 2
1433+
# TODO(EA2D): transpose will be unnecessary with 2D EAs
1434+
new_values = algos.diff(self.values.T, n, axis=0).T
14331435
return [self.make_block(values=new_values)]
14341436

14351437
def shift(
@@ -2067,12 +2069,6 @@ def slice_block_rows(self, slicer: slice) -> Self:
20672069
new_values = self.values[slicer]
20682070
return type(self)(new_values, self._mgr_locs, ndim=self.ndim, refs=self.refs)
20692071

2070-
def diff(self, n: int, axis: AxisInt = 1) -> list[Block]:
2071-
# only reached with ndim == 2 and axis == 1
2072-
# TODO(EA2D): Can share with NDArrayBackedExtensionBlock
2073-
new_values = algos.diff(self.values, n, axis=0)
2074-
return [self.make_block(values=new_values)]
2075-
20762072
def shift(
20772073
self, periods: int, axis: AxisInt = 0, fill_value: Any = None
20782074
) -> list[Block]:
@@ -2191,32 +2187,6 @@ def is_view(self) -> bool:
21912187
# check the ndarray values of the DatetimeIndex values
21922188
return self.values._ndarray.base is not None
21932189

2194-
def diff(self, n: int, axis: AxisInt = 0) -> list[Block]:
2195-
"""
2196-
1st discrete difference.
2197-
2198-
Parameters
2199-
----------
2200-
n : int
2201-
Number of periods to diff.
2202-
axis : int, default 0
2203-
Axis to diff upon.
2204-
2205-
Returns
2206-
-------
2207-
A list with a new Block.
2208-
2209-
Notes
2210-
-----
2211-
The arguments here are mimicking shift so they are called correctly
2212-
by apply.
2213-
"""
2214-
# only reached with ndim == 2 and axis == 1
2215-
values = self.values
2216-
2217-
new_values = values - values.shift(n, axis=axis)
2218-
return [self.make_block(new_values)]
2219-
22202190
def shift(
22212191
self, periods: int, axis: AxisInt = 0, fill_value: Any = None
22222192
) -> list[Block]:

pandas/core/internals/managers.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -411,10 +411,9 @@ def putmask(self, mask, new, align: bool = True) -> Self:
411411
using_cow=using_copy_on_write(),
412412
)
413413

414-
def diff(self, n: int, axis: AxisInt) -> Self:
415-
# only reached with self.ndim == 2 and axis == 1
416-
axis = self._normalize_axis(axis)
417-
return self.apply("diff", n=n, axis=axis)
414+
def diff(self, n: int) -> Self:
415+
# only reached with self.ndim == 2
416+
return self.apply("diff", n=n)
418417

419418
def interpolate(self, inplace: bool, **kwargs) -> Self:
420419
return self.apply(

0 commit comments

Comments
 (0)