Python Forum
FutureWarning: Logical ops (and, or, xor) between Pandas objects and dtype-less seque - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: FutureWarning: Logical ops (and, or, xor) between Pandas objects and dtype-less seque (/thread-40644.html)



FutureWarning: Logical ops (and, or, xor) between Pandas objects and dtype-less seque - NewBiee - Aug-31-2023

I have had this method on my code, it's been working fine, but as of today my code fails on this method:

def get_flags(series, flags, regex_flag):
    """ Identifies whether the series contains the flags under consideration.

    Args:
        series (pandas.DataFrame.Series): Series against which regular expressions will be matched.
        flags (list): List of regular expressions to be evaluated
    """
    m = [False] * len(series)
    series = series.astype(str).str.lower()
    for i, flag in enumerate(flags):
        flag = flag.lower()
        m = m | series.astype(str).str.contains(
            flag.lower(), regex=regex_flag, case=True,
        )
    return m
Now I get this error and my code stops running:

Output:
FutureWarning: Logical ops (and, or, xor) between Pandas objects and dtype-less sequences (e.g. list, tuple) are deprecated and will raise in a future version. Wrap the object in a Series, Index, or np.array before operating instead. m = m | series.astype(str).str.contains(
Please help.


RE: FutureWarning: Logical ops (and, or, xor) between Pandas objects and dtype-less seque - deanhystad - Aug-31-2023

I think this is the problem:
m = [False] * len(series)
This is not a "Series, Index, or np.array". Try m = numpy.full((len(series), False).


RE: FutureWarning: Logical ops (and, or, xor) between Pandas objects and dtype-less seque - NewBiee - Sep-01-2023

(Aug-31-2023, 03:53 PM)deanhystad Wrote: I think this is the problem:
m = [False] * len(series)
This is not a "Series, Index, or np.array". Try m = numpy.full((len(series), False).

Hi, Thank you.

I tried this but my code started flagging errors(red line) under certain words, I've highlighted all places by putting them inside the square brackets.

def get_flags(series, flags, regex_flag):
    """ Identifies whether the series contains the flags under consideration.
 
    Args:
        series (pandas.DataFrame.Series): Series against which regular expressions will be matched.
        flags (list): List of regular expressions to be evaluated
    """
    #m = [False] * len(series[)]
    m = np.full((len(series), False)
    series = [series].astype(str).str.lower([)]
    for i, flag in enumerate(flags):
        flag = [flag].lower()
        m = m | series.astype(str).str.contains(
            flag.lower(), regex=regex_flag, case=True,
        )
    return m



RE: FutureWarning: Logical ops (and, or, xor) between Pandas objects and dtype-less seque - deanhystad - Sep-01-2023

That would be because of the extra "(" in m = np.full((len(series), False)

I don.t think you need to do this: series = series.astype(str).str.lower()

Are there going to be things in the series that aren't strings? If so, how can you lower() them?

Just use : series = series.str.lower()


RE: FutureWarning: Logical ops (and, or, xor) between Pandas objects and dtype-less seque - NewBiee - Sep-12-2023

Thank you for your help so far.

I've made changes to my code as per your help:

def get_flags(series, flags, regex_flag):
    """ Identifies whether the series contains the flags under consideration.

    Args:
        series (pandas.DataFrame.Series): Series against which regular expressions will be matched.
        flags (list): List of regular expressions to be evaluated
    """
    m = [False] * len(series)
    series = series.str.lower()
    for i, flag in enumerate(flags):
        flag = flag.lower()
        m = m | series.astype(str).str.contains(
            flag.lower(), regex=regex_flag, case=True,
        )
    return m
But I'm now getting this error below, I think there's something I'm missing there, like I missed extra "(" before, please help:

Output:
from .extract_meta_lib.extract_flags import extract_flags File "\extract_flags.py", line 20 series = series.astype(str).str.lower() ^ SyntaxError: invalid syntax



RE: FutureWarning: Logical ops (and, or, xor) between Pandas objects and dtype-less seque - deanhystad - Sep-12-2023

The error message does not match the code you have posted. Please post the code that generates the error message.

error message says:
series = series.astype(str).str.lower()
posted code says:
series = series.str.lower()