Pandas V0.20 Returns Notimplemented When Multiplying Dataframe Columns
In attempt to answer another question I've been playing around with column-wise multiplication operations in pandas. A = pd.DataFrame({'Col1' : [1, 2, 3], 'Col2' : [2, 3, 4]}) B =
Solution 1:
You can just do:
A.apply(B.Col1.__mul__,0)
Which returns what you're after.
The difference is that B.Col1.values.__mul__
is calling the numpy slot function, but B.Col1.__mul__
is calling a pandas method.
Likely the pandas method was written to avoid some low level headache from numpy:
>>>print(inspect.getsource(pd.Series.__mul__))
def wrapper(left, right, name=name, na_op=na_op):
if isinstance(right, pd.DataFrame):
return NotImplemented
left, right= _align_method_SERIES(left, right)
converted = _Op.get_op(left, right, name, na_op)
left, right= converted.left, converted.right
lvalues, rvalues = converted.lvalues, converted.rvalues
dtype = converted.dtype
wrap_results = converted.wrap_results
na_op = converted.na_op
if isinstance(rvalues, ABCSeries):
name = _maybe_match_name(left, rvalues)
lvalues = getattr(lvalues, 'values', lvalues)
rvalues = getattr(rvalues, 'values', rvalues)
# _Op aligns leftandrightelse:
name = left.name
if (hasattr(lvalues, 'values') andnot isinstance(lvalues, pd.DatetimeIndex)):
lvalues = lvalues.values
result= wrap_results(safe_na_op(lvalues, rvalues))
return construct_result(
left,
result,
index=left.index,
name=name,
dtype=dtype,
)
Can't find source on the np slot function, but it's likely something similar to this
Post a Comment for "Pandas V0.20 Returns Notimplemented When Multiplying Dataframe Columns"