Skip to content

Commit a6c0d02

Browse files
POC improved column-wise ops
1 parent 32b4710 commit a6c0d02

File tree

5 files changed

+25
-6
lines changed

5 files changed

+25
-6
lines changed

pandas/core/frame.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,8 +1911,10 @@ def from_items(cls, items, columns=None, orient="columns"):
19111911
raise ValueError("'orient' must be either 'columns' or 'index'")
19121912

19131913
@classmethod
1914-
def _from_arrays(cls, arrays, columns, index, dtype=None):
1915-
mgr = arrays_to_mgr(arrays, columns, index, columns, dtype=dtype)
1914+
def _from_arrays(cls, arrays, columns, index, dtype=None, fastpath=False):
1915+
mgr = arrays_to_mgr(
1916+
arrays, columns, index, columns, dtype=dtype, fastpath=fastpath
1917+
)
19161918
return cls(mgr)
19171919

19181920
def to_sparse(self, fill_value=None, kind="block"):
@@ -5294,6 +5296,13 @@ def reorder_levels(self, order, axis=0):
52945296
# ----------------------------------------------------------------------
52955297
# Arithmetic / combination related
52965298

5299+
def _get_arrays(self):
5300+
res = []
5301+
for i in range(len(self.columns)):
5302+
values = self._data.iget_values(i)
5303+
res.append(values)
5304+
return res
5305+
52975306
def _combine_frame(self, other, func, fill_value=None, level=None):
52985307
this, other = self.align(other, join="outer", level=level, copy=False)
52995308
new_index, new_columns = this.index, this.columns

pandas/core/internals/blocks.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3043,7 +3043,9 @@ def get_block_type(values, dtype=None):
30433043
dtype = dtype or values.dtype
30443044
vtype = dtype.type
30453045

3046-
if is_sparse(dtype):
3046+
if dtype == np.bool_:
3047+
cls = BoolBlock
3048+
elif is_sparse(dtype):
30473049
# Need this first(ish) so that Sparse[datetime] is sparse
30483050
cls = ExtensionBlock
30493051
elif is_categorical(values):

pandas/core/internals/construction.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
# BlockManager Interface
5656

5757

58-
def arrays_to_mgr(arrays, arr_names, index, columns, dtype=None):
58+
def arrays_to_mgr(arrays, arr_names, index, columns, dtype=None, fastpath=False):
5959
"""
6060
Segregate Series based on type and coerce into matrices.
6161
@@ -68,7 +68,8 @@ def arrays_to_mgr(arrays, arr_names, index, columns, dtype=None):
6868
index = ensure_index(index)
6969

7070
# don't force copy because getting jammed in an ndarray anyway
71-
arrays = _homogenize(arrays, index, dtype)
71+
if not fastpath:
72+
arrays = _homogenize(arrays, index, dtype)
7273

7374
# from BlockManager perspective
7475
axes = [ensure_index(columns), index]

pandas/core/internals/managers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,11 @@ def iget(self, i):
987987
self.axes[1],
988988
)
989989

990+
def iget_values(self, i):
991+
block = self.blocks[self._blknos[i]]
992+
values = block.iget(self._blklocs[i])
993+
return values
994+
990995
def delete(self, item):
991996
"""
992997
Delete selected item (items if non-unique) in-place.

pandas/core/ops/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,9 @@ def dispatch_to_series(left, right, func, str_rep=None, axis=None):
485485
if lib.is_scalar(right) or np.ndim(right) == 0:
486486

487487
def column_op(a, b):
488-
return {i: func(a.iloc[:, i], b) for i in range(len(a.columns))}
488+
# return {i: func(a.iloc[:, i], b) for i in range(len(a.columns))}
489+
res = [func(col, b) for col in a._get_arrays()]
490+
return left._from_arrays(res, left.columns, left.index, fastpath=True)
489491

490492
elif isinstance(right, ABCDataFrame):
491493
assert right._indexed_same(left)

0 commit comments

Comments
 (0)