AlexandreDecan / portion

portion, a Python library providing data structure and operations for intervals.
GNU Lesser General Public License v3.0
476 stars 34 forks source link

Add example for pandas in README #84

Closed mikel-brostrom closed 1 year ago

mikel-brostrom commented 1 year ago

I just used your library with pandas, but I could not find a single example on how to do this, so I though it would be nice to leave one here. I applied an IntervalDict on a column:

intervals = IntervalDict(
    {
        popen(    -inf,    5):   lambda y:  0,
        pclosedopen( 5,    8.6): lambda y: -0.1 * y + 10, 
        pclosedopen( 8.6, 18):   lambda y: -0.1 * y + 4.39,
        pclosedopen(18,   25):   lambda y: -0.2 * y + 3,
        pclosedopen(25,  inf):   lambda y:  0.8
    }
)
df['Y_interval'] = df['Y'].apply(lambda y: intervals[y](y))
AlexandreDecan commented 1 year ago

Hello,

Thanks for this. But could you clarify the purpose and context of this piece of code? I don't see exactly which problem it addresses.

mikel-brostrom commented 1 year ago

It is just an example on how to apply an interval dict on a pandas dataframe column. Nothing more

AlexandreDecan commented 1 year ago

Ok, so a kind of value-based map function that, depending on the value, apply a different function on a column. Right? To some extent, the goal of this example is to show how to apply different functions without having to rely on combinations of "where"?

mikel-brostrom commented 1 year ago

Ok, so a kind of value-based map function that, depending on the value, apply a different function on a column.

Exactly

AlexandreDecan commented 1 year ago

Ok, thanks ! Let's keep the "information" label for this issue, and I'll refer to it in a (not yet created) "recipe" section of the documentation.