Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pandas
python

How to construct an IntervalArray object from an array of splits

Onyejiaku Theophilus Chidalu

Overview

The from_breaks() function in pandas is used to create an IntervalArray object from an array of splits.

Note: IntervalArray is an object containing interval data such that they are closed on the same side.

Syntax

The from_breaks() function has the following syntax:

classmethod IntervalArray.from_breaks(breaks, closed='right', copy=False, dtype=None)
Syntax for the from_breaks() function

Parameters

The from_breaks() function takes the following parameter values:

  • breaks: This represents the left and right bounds of the IntervalArray object to be formed. This is a required parameter.
  • closed: This takes any of the following values: 'left', 'right', 'both', 'neither'. These values are used to specify if the interval of the object is closed on the left side, right side, both sides, or neither side. The default value is 'right'.  This is an optional parameter.
  • copy: This takes a boolean value that determines if the data should be copied or not. This is an optional parameter.
  • dtype: This determines the data type of the output object.

Return value

The from_breaks() function returns an IntervalArray object.

Example

# A code to illustrate the from_breaks() function in Python

# importing the pandas library
import pandas as pd

# creating an array of splits
a = [1, 2, 3, 4, 5]

# printing a 
print("This is the input array: ", "\n", a)

print("\n")
# creating an IntervalArray object from a
b = pd.arrays.IntervalArray.from_breaks(a)

# printing the IntervalArray object
print("This is an IntervalArray object: ","\n", b)
Implementing the from_breaks() function

Explanation

  • Line 4: We import the pandas library.
  • Line 7: We create an array of splits a.
  • Line 10: We print the value of a.
  • Line 14: From the values of a, we create an IntervalArray object b by using the from_breaks() function.
  • Line 17: We print the IntervalArray object b.

RELATED TAGS

pandas
python

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring