Interval datatype in Pandas - find midpoint, left, center etc

Viewed 18433

In pandas 20.1, with the interval type, is it possible to find the midpoint, left or center values in a series.

For example:

  1. Create an interval datatype column, and perform some aggregation calculations over these intervals:

    df_Stats = df.groupby(['month',pd.cut(df['Distances'], np.arange(0, 135,1))]).agg(aggregations)
    

This returns df_Stats with an interval column datatype : df['Distances']

  1. Now I want to associate the left end of the interval to the result of these aggregations using a series function:

    df['LeftEnd'] = df['Distances'].left
    

However, I can run this element wise:

    df.loc[0]['LeftEnd'] = df.loc[0]['Distances'].left

This works. Thoughts?

2 Answers

Say 'cut' is the column name after performing pd.cut.

instead of ->

 df['LeftEnd'] = df['Distances'].left

perform one of the following -->

 df['LeftEnd'] = df['cut'].apply(lambda x: x.left)

 df['LeftEnd'] = df['cut'].apply(lambda x: x.left).astype(str)
Related