-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
Support multiplication of pd.ArrowDtype(pa.string()) and integral value where integral value is a series #56538
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -695,22 +695,23 @@ def _evaluate_op_method(self, other, op, arrow_funcs): | |
other = self._box_pa(other) | ||
|
||
if pa.types.is_string(pa_type) or pa.types.is_binary(pa_type): | ||
if op in [operator.add, roperator.radd, operator.mul, roperator.rmul]: | ||
if op in [operator.add, roperator.radd]: | ||
sep = pa.scalar("", type=pa_type) | ||
if op is operator.add: | ||
result = pc.binary_join_element_wise(self._pa_array, other, sep) | ||
elif op is roperator.radd: | ||
result = pc.binary_join_element_wise(other, self._pa_array, sep) | ||
else: | ||
if not ( | ||
isinstance(other, pa.Scalar) and pa.types.is_integer(other.type) | ||
): | ||
raise TypeError("Can only string multiply by an integer.") | ||
result = pc.binary_join_element_wise( | ||
*([self._pa_array] * other.as_py()), sep | ||
) | ||
return type(self)(result) | ||
|
||
elif op in [operator.mul, roperator.rmul]: | ||
result = type(self)._evaluate_binary_repeat(self._pa_array, other) | ||
return type(self)(result) | ||
elif ( | ||
pa.types.is_integer(pa_type) | ||
and (pa.types.is_string(other.type) or pa.types.is_binary(other.type)) | ||
and op in [operator.mul, roperator.rmul] | ||
): | ||
result = type(self)._evaluate_binary_repeat(other, self._pa_array) | ||
return type(self)(result) | ||
if ( | ||
isinstance(other, pa.Scalar) | ||
and pc.is_null(other).as_py() | ||
|
@@ -726,6 +727,13 @@ def _evaluate_op_method(self, other, op, arrow_funcs): | |
result = pc_func(self._pa_array, other) | ||
return type(self)(result) | ||
|
||
@staticmethod | ||
def _evaluate_binary_repeat(binary, integral): | ||
if not pa.types.is_integer(integral.type): | ||
rohanjain101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raise TypeError("Can only string multiply by an integer.") | ||
pa_integral = pc.if_else(pc.less(integral, 0), 0, integral) | ||
return pc.binary_repeat(binary, pa_integral) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you inline these where needed. I don't think a whole new function is needed for it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mroeschke, can do, but there's 2 callers of it, so if we remove the function, the logic for calculating the repeat count to pass to binary_repeat would need to be repeated in both callers. Additionally, I believe the helper function simplifies the code because there's 2 scenarios:
When we call binary_repeat, we need to always ensure the binary is the left arg and the integral is the right arg, imo the helper function simplifies the code and makes it a bit more readable. Let me know if you would still prefer logic of _evaluate_binary_repeat to be inlined, or if I misunderstood your suggestion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would still prefer to inline this for now. We don't really use staticmethods in the codebase There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, inlined. |
||
|
||
def _logical_method(self, other, op): | ||
# For integer types `^`, `|`, `&` are bitwise operators and return | ||
# integer types. Otherwise these are boolean ops. | ||
|
Uh oh!
There was an error while loading. Please reload this page.