Ilan12
Ilan12

Reputation: 81

Creating list of pandas.Intervals

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)

Upvotes: 1

Views: 2458

Answers (3)

Corralien
Corralien

Reputation: 120559

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]')

Upvotes: 3

René
René

Reputation: 4827

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]

Upvotes: 0

Ilan12
Ilan12

Reputation: 81

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)

Upvotes: 1

Related Questions