Home > Blockchain >  Creating list of pandas.Intervals
Creating list of pandas.Intervals

Time:10-24

I am use a vectorize code to create a list of pandas.Intervals.

Example:

a = np.array([1, 5, 10])
b = np.array([3, 8, 12])

Desired output:

enter image description here

What I have tried:

A:

pd.Intervals(a,b)

B:

df['c'] = tuple(np.array([a, b]).T)
df['c'] = df['c'].astype(pd.Intervals)

CodePudding user response:

I used Riley reference to find the solution: using pandas.arrays.IntervalArray.from_arrays

The solution:

a = np.array([1, 5, 10])
b = np.array([3, 8, 12])
c = pd.arrays.IntervalArray.from_arrays(a, b)

CodePudding user response:

You can use IntervalIndex instead of IntervalArray to have more methods:

c = pd.IntervalIndex.from_arrays(a, b)
print(c)

# Output:
IntervalIndex([(1, 3], (5, 8], (10, 12]], dtype='interval[int64, right]')

CodePudding user response:

Maybe this will work for you:

import pandas as pd
import numpy as np

a = np.array([1, 5, 10])
b = np.array([3, 8, 12])

df = pd.DataFrame({
    'a': a, 
    'b': b, 
    'c': [pd.Interval(*boundaries) for boundaries in zip(a, b)]
})

print(df)

Result:

    a   b         c
0   1   3    (1, 3]
1   5   8    (5, 8]
2  10  12  (10, 12]
  • Related