BUG: pandas.core.ops.dispatch_to_extension_op fails with UnboundLocalError
See original GitHub issueCode Sample, a copy-pastable example if possible
This bug requires an ExtensionArray that is not backed with a numpy array to occur.
import pandas as pd
import fletcher as fr
s = pd.Series(fr.FletcherArray(TEST_ARRAY))
assert (s.T == s).all()
Problem description
Looking at the code the value of new_right
is only set when the the values of the ExtensionArray are a np.ndarray
. With fletcher, this is not the case.
Error message
> assert (s.T == s).all()
tests/test_pandas_integration.py:139:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../pandas/pandas/core/ops.py:1433: in wrapper
return dispatch_to_extension_op(op, self, other)
../pandas/pandas/core/ops.py:1163: in dispatch_to_extension_op
res_values = op(new_left, new_right)
../pandas/pandas/core/ops.py:1433: in wrapper
return dispatch_to_extension_op(op, self, other)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
op = <built-in function eq>, left = 0 Test
1 string
2 None
dtype: fletcher[string], right = <fletcher.base.FletcherArray object at 0x112ce7518>
def dispatch_to_extension_op(op, left, right):
"""
Assume that left or right is a Series backed by an ExtensionArray,
apply the operator defined by op.
"""
# The op calls will raise TypeError if the op is not defined
# on the ExtensionArray
# TODO(jreback)
# we need to listify to avoid ndarray, or non-same-type extension array
# dispatching
if is_extension_array_dtype(left):
new_left = left.values
if isinstance(right, np.ndarray):
# handle numpy scalars, this is a PITA
# TODO(jreback)
new_right = lib.item_from_zerodim(right)
if is_scalar(new_right):
new_right = [new_right]
new_right = list(new_right)
elif is_extension_array_dtype(right) and type(left) != type(right):
> new_right = list(new_right)
E UnboundLocalError: local variable 'new_right' referenced before assignment
../pandas/pandas/core/ops.py:1154: UnboundLocalError
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (10 by maintainers)
Top Results From Across the Web
BUG: UnboundLocalError when missing_kwds is used #2210
Maybe some complete sentence on the issue: The missing_kwds defines how missing data should be styled on the plotted map and does this...
Read more >pandas apply() results in UnboundLocalError - Stack Overflow
This implementation uses the unique values in the column to create the map, so there won't be an issue with "local variable 'value'...
Read more >Changelog — Python 3.11.1 documentation
Prevents the creation of generators and closures from being observable to Python and C extensions, restoring the behavior of 3.10 and earlier. gh-94192:...
Read more >What's new in 1.4.0 (January 22, 2022) - Pandas
site-packages/pandas/core/indexing.py:1951: SettingWithCopyWarning: A value is trying ... The error raised when an optional dependency can't be imported now ...
Read more >SciPy 1.4.0 Release Notes — SciPy v1.9.3 Manual
Sparse matrix reshape now raises an error if shape is not two-dimensional, rather than guessing what was meant. The behavior is now the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, this is a thing I need to write an example ExtensionArray for in the Pandas tests. I’ve been reporting these bugs but it is always hard to make reproducible tests. I hope to find enough time in a single block soon to write such an example ExtensionArray. Then it will be simpler to fix them with a reproducing test.
@xhochy BTW, in the meantime, there is a non-ndarray backed example ExtensionArray (based on a pyarrow array) that is used for testing (since this was a problem for you to write a test).