Python Forum
FutureWarning: Logical ops (and, or, xor) between Pandas objects and dtype-less seque
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
FutureWarning: Logical ops (and, or, xor) between Pandas objects and dtype-less seque
#1
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.
Reply
#2
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).
Reply
#3
(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
Reply
#4
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()
Reply
#5
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
Reply
#6
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Data dtype error according to the rule "safe" AndreasMavro 5 9,198 Feb-27-2020, 10:46 PM
Last Post: Pama
  dtype in not working in mad() function ift38375 8 4,008 Jul-22-2019, 02:53 AM
Last Post: scidam
  ValueError: Input contains infinity or a value too large for dtype('float64') Rabah_r 1 12,927 Apr-06-2019, 11:08 AM
Last Post: scidam
  numpy dtype anomaly bluefrog 4 3,524 Nov-07-2018, 12:37 AM
Last Post: bluefrog

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020