Skip to content

Commit a2cdd50

Browse files
INT: provide helpers for accessing the values of DataFrame columns (#33252)
1 parent efa85af commit a2cdd50

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

pandas/core/frame.py

+32-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
FrozenSet,
2424
Hashable,
2525
Iterable,
26+
Iterator,
2627
List,
2728
Optional,
2829
Sequence,
@@ -40,7 +41,16 @@
4041
from pandas._config import get_option
4142

4243
from pandas._libs import algos as libalgos, lib, properties
43-
from pandas._typing import Axes, Axis, Dtype, FilePathOrBuffer, Label, Level, Renamer
44+
from pandas._typing import (
45+
ArrayLike,
46+
Axes,
47+
Axis,
48+
Dtype,
49+
FilePathOrBuffer,
50+
Label,
51+
Level,
52+
Renamer,
53+
)
4454
from pandas.compat import PY37
4555
from pandas.compat._optional import import_optional_dependency
4656
from pandas.compat.numpy import function as nv
@@ -2583,6 +2593,21 @@ def _ixs(self, i: int, axis: int = 0):
25832593

25842594
return result
25852595

2596+
def _get_column_array(self, i: int) -> ArrayLike:
2597+
"""
2598+
Get the values of the i'th column (ndarray or ExtensionArray, as stored
2599+
in the Block)
2600+
"""
2601+
return self._data.iget_values(i)
2602+
2603+
def _iter_column_arrays(self) -> Iterator[ArrayLike]:
2604+
"""
2605+
Iterate over the arrays of all columns in order.
2606+
This returns the values as stored in the Block (ndarray or ExtensionArray).
2607+
"""
2608+
for i in range(len(self.columns)):
2609+
yield self._get_column_array(i)
2610+
25862611
def __getitem__(self, key):
25872612
key = lib.item_from_zerodim(key)
25882613
key = com.apply_if_callable(key, self)
@@ -8046,8 +8071,12 @@ def _reduce(
80468071

80478072
assert filter_type is None or filter_type == "bool", filter_type
80488073

8049-
dtype_is_dt = self.dtypes.apply(
8050-
lambda x: is_datetime64_any_dtype(x) or is_period_dtype(x)
8074+
dtype_is_dt = np.array(
8075+
[
8076+
is_datetime64_any_dtype(values.dtype) or is_period_dtype(values.dtype)
8077+
for values in self._iter_column_arrays()
8078+
],
8079+
dtype=bool,
80518080
)
80528081
if numeric_only is None and name in ["mean", "median"] and dtype_is_dt.any():
80538082
warnings.warn(

pandas/core/internals/managers.py

+8
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,14 @@ def iget(self, i: int) -> "SingleBlockManager":
985985
self.axes[1],
986986
)
987987

988+
def iget_values(self, i: int) -> ArrayLike:
989+
"""
990+
Return the data for column i as the values (ndarray or ExtensionArray).
991+
"""
992+
block = self.blocks[self.blknos[i]]
993+
values = block.iget(self.blklocs[i])
994+
return values
995+
988996
def idelete(self, indexer):
989997
"""
990998
Delete selected locations in-place (new block and array, same BlockManager)

0 commit comments

Comments
 (0)