'python, pandas, I have data like <16 * MonthEnds>, how to compare it to an integer? [duplicate]

my data looks like this:

<16 * MonthEnds>
<19 * MonthEnds>
<23 * MonthEnds>
<12 * MonthEnds>
<24 * MonthEnds>
<13 * MonthEnds>

and i want to do something like:

if Duration<12:
    do something

but i can't find away to compare the duration with an integer

i always get an error

TypeError: '<' not supported between instances of 'pandas._libs.tslibs.offsets.MonthEnd' and 'int'


Solution 1:[1]

You can convert MonthEnds offset to integer:

df['Duration'] = df['Duration'].astype(str).str.extract('(\d+)').astype(int)

# OR

df['Duration'] = df['Duration'].map(lambda x: getattr(x, 'n')) < 12

Output:

>>> df
   Duration
0        16
1        19
2        23
3        12
4        24
5        13

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Corralien