Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pandas
python

How to construct an IntervalArray object from an array of tuples

Onyejiaku Theophilus Chidalu

Overview

The from_tuples() function in pandas creates an IntervalArray object from an array of tuples.

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

Syntax

The from_tuples() function has the following syntax:

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

Parameters

The from_tuples() function takes the following parameter values:

  • data: This is an array of tuples from which the IntervalArray object is to be formed. This is a required parameter.
  • closed: This takes any of the following: ‘left’, ‘right’, ‘both’, ‘neither’. They are used to specify if the interval of the object is closed on the left side, right side, both, or neither. The default value is 'right'. This is an optional parameter.
  • copy: This takes a Boolean value indicating if the data should be copied or not. This is an optional parameter.
  • dtype: This takes the data type of the output object.

Return value

The from_tuples() function returns an IntervalArray object.

Example

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

# importing the pandas library
import pandas as pd

# creating an array of tuples
a = [(1, 2), (3, 4)]

# printing a 
print("This is an array of tuples: ", "\n", a)

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

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

Explanation

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

RELATED TAGS

pandas
python

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring