Skip to content

Pandas: index set operations #2

@setaur

Description

@setaur

Hi.
I'm currently working through your Python Fundamentals course, and I came across a problem in the Pandas chapter.
In notebook https://github.com/fbaptiste/python-fundamentals/blob/main/30%20-%20Pandas/02%20-%20Indexes/Indexes.ipynb :

idx_1 = pd.Index(['a', 'b', 'c'])
idx_2 = pd.Index(['c', 'd', 'e'])

idx_1 & idx_2

Index(['c'], dtype='object')

idx_1 | idx_2

Index(['a', 'b', 'c', 'd', 'e'], dtype='object')

However, when I try those, I get an error.

idx_1 & idx_2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File /home/user/python-udemy-course/poligon/02/srodowisko/lib/python3.13/site-packages/pandas/core/ops/array_ops.py:362, in na_logical_op(x, y, op)
    353 try:
    354     # For exposition, write:
    355     #  yarr = isinstance(y, np.ndarray)
   (...)    360     # Then Cases where this goes through without raising include:
    361     #  (xint or xbool) and (yint or bool)
--> 362     result = op(x, y)
    363 except TypeError:

TypeError: unsupported operand type(s) for &: 'str' and 'str'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
Cell In[6], line 1
----> 1 idx_1 & idx_2

File /home/user/python-udemy-course/poligon/02/srodowisko/lib/python3.13/site-packages/pandas/core/ops/common.py:76, in _unpack_zerodim_and_defer.<locals>.new_method(self, other)
     72             return NotImplemented
     74 other = item_from_zerodim(other)
---> 76 return method(self, other)

File /home/user/python-udemy-course/poligon/02/srodowisko/lib/python3.13/site-packages/pandas/core/arraylike.py:70, in OpsMixin.__and__(self, other)
     68 @unpack_zerodim_and_defer("__and__")
     69 def __and__(self, other):
---> 70     return self._logical_method(other, operator.and_)

File /home/user/python-udemy-course/poligon/02/srodowisko/lib/python3.13/site-packages/pandas/core/indexes/base.py:7215, in Index._logical_method(self, other, op)
   7212 lvalues = self._values
   7213 rvalues = extract_array(other, extract_numpy=True, extract_range=True)
-> 7215 res_values = ops.logical_op(lvalues, rvalues, op)
   7216 return self._construct_result(res_values, name=res_name)

File /home/user/python-udemy-course/poligon/02/srodowisko/lib/python3.13/site-packages/pandas/core/ops/array_ops.py:454, in logical_op(left, right, op)
    450 else:
    451     # i.e. scalar
    452     is_other_int_dtype = lib.is_integer(rvalues)
--> 454 res_values = na_logical_op(lvalues, rvalues, op)
    456 # For int vs int `^`, `|`, `&` are bitwise operators and return
    457 #   integer dtypes.  Otherwise these are boolean ops
    458 if not (left.dtype.kind in "iu" and is_other_int_dtype):

File /home/user/python-udemy-course/poligon/02/srodowisko/lib/python3.13/site-packages/pandas/core/ops/array_ops.py:369, in na_logical_op(x, y, op)
    367     x = ensure_object(x)
    368     y = ensure_object(y)
--> 369     result = libops.vec_binop(x.ravel(), y.ravel(), op)
    370 else:
    371     # let null fall thru
    372     assert lib.is_scalar(y)

File ops.pyx:252, in pandas._libs.ops.vec_binop()

File ops.pyx:245, in pandas._libs.ops.vec_binop()

TypeError: unsupported operand type(s) for &: 'str' and 'str'
idx_1 | idx_2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File /home/user/python-udemy-course/poligon/02/srodowisko/lib/python3.13/site-packages/pandas/core/ops/array_ops.py:362, in na_logical_op(x, y, op)
    353 try:
    354     # For exposition, write:
    355     #  yarr = isinstance(y, np.ndarray)
   (...)    360     # Then Cases where this goes through without raising include:
    361     #  (xint or xbool) and (yint or bool)
--> 362     result = op(x, y)
    363 except TypeError:

TypeError: unsupported operand type(s) for |: 'str' and 'str'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
Cell In[7], line 1
----> 1 idx_1 | idx_2

File /home/user/python-udemy-course/poligon/02/srodowisko/lib/python3.13/site-packages/pandas/core/ops/common.py:76, in _unpack_zerodim_and_defer.<locals>.new_method(self, other)
     72             return NotImplemented
     74 other = item_from_zerodim(other)
---> 76 return method(self, other)

File /home/user/python-udemy-course/poligon/02/srodowisko/lib/python3.13/site-packages/pandas/core/arraylike.py:78, in OpsMixin.__or__(self, other)
     76 @unpack_zerodim_and_defer("__or__")
     77 def __or__(self, other):
---> 78     return self._logical_method(other, operator.or_)

File /home/user/python-udemy-course/poligon/02/srodowisko/lib/python3.13/site-packages/pandas/core/indexes/base.py:7215, in Index._logical_method(self, other, op)
   7212 lvalues = self._values
   7213 rvalues = extract_array(other, extract_numpy=True, extract_range=True)
-> 7215 res_values = ops.logical_op(lvalues, rvalues, op)
   7216 return self._construct_result(res_values, name=res_name)

File /home/user/python-udemy-course/poligon/02/srodowisko/lib/python3.13/site-packages/pandas/core/ops/array_ops.py:454, in logical_op(left, right, op)
    450 else:
    451     # i.e. scalar
    452     is_other_int_dtype = lib.is_integer(rvalues)
--> 454 res_values = na_logical_op(lvalues, rvalues, op)
    456 # For int vs int `^`, `|`, `&` are bitwise operators and return
    457 #   integer dtypes.  Otherwise these are boolean ops
    458 if not (left.dtype.kind in "iu" and is_other_int_dtype):

File /home/user/python-udemy-course/poligon/02/srodowisko/lib/python3.13/site-packages/pandas/core/ops/array_ops.py:369, in na_logical_op(x, y, op)
    367     x = ensure_object(x)
    368     y = ensure_object(y)
--> 369     result = libops.vec_binop(x.ravel(), y.ravel(), op)
    370 else:
    371     # let null fall thru
    372     assert lib.is_scalar(y)

File ops.pyx:252, in pandas._libs.ops.vec_binop()

File ops.pyx:245, in pandas._libs.ops.vec_binop()

TypeError: unsupported operand type(s) for |: 'str' and 'str'

both in the Jupyter notebook and on the bare Python.

On the other hand there are methods pandas.Index.intersection and pandas.Index.union, which are working fine:

>>> idx_1.union(idx_2)
Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
>>> idx_1.intersection(idx_2)
Index(['c'], dtype='object')

Maybe something changed in Python and/or in Pandas?
I'm using Python 3.13.5 and Pandas Version: 2.2.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions