The **numpy.modf() **function in Python is a part of the NumPy library that can be used to separate the fractional and integer parts of an array of floating-point numbers. It returns two arrays – one containing the fractional parts and another containing the integer parts.

In this article, we will understand Python **numpy.modf()** function, its syntax, and we will also see the variety of techniques using which we can separate the fractional and integer parts of an array of floating-point numbers with the help of this function. Let’s get started.

*Also Read: numpy.where() in Python*

## Syntax of numpy.modf() Function

Let us get started by understanding the fundamental constituents of the* ***numpy.modf()** function with the help of its syntax given below.

**Syntax:**

```
numpy.modf(x, /, out1=None, out2=None, *, where=True, casting='same_kind', order='K', dtype=None)
```

**Parameters:**

**x:**This is the input array for which you want to separate the fractional and integer parts.**out1 (optional):**This is an optional parameter that specifies the location where the fractional parts should be stored. If not provided, a new array will be created.**out2 (optional):**This is an optional parameter that specifies the location where the integer parts should be stored. If not provided, a new array will be created.**where (optional):**This parameter is used for conditional execution. If provided, only the elements where the condition is True will be included in the result.**casting (optional):**This parameter controls what kind of data casting may occur. The default is ‘same_kind’, which means that only casting between similar kinds (e.g., float to float) is allowed.**order (optional):**Specifies the memory layout of the result. The default is ‘K’ which means the order is determined by the input array’s memory layout.**dtype (optional):**This parameter allows us to set the data type of the output array. If not specified, it is determined by the data types of the input arrays.

**Return:**

The **numpy.modf()** function returns two arrays, let’s say** out1** and **out2** where the **out1** array contains the fractional parts of the input array and the** out2** array contains the integer parts.

## Examples of numpy.modf() Function

Let us now look at some examples to demonstrate the use of **numpy.modf()** function in Python.

### Separating Integer and Fractional Part from NumPy Array

In this section, we will try to separate integer and fractional parts from a one-dimensional NumPy array.

```
import numpy as np
ar = np.array([13.11, 24.12, 21.0, 235.57, 88.10])
fract = np.empty_like(ar)
inte = np.empty_like(ar)
np.modf(ar, out=(fract, inte))
print("The Original array:", ar)
print("Fractional part of the array:", fract)
print("Integer part of the array:", inte)
```

Here we first imported the NumPy library as **np** then using it we created a 1-D array that contains integer and fractional parts and saved it into a variable called **ar**.

Then by using the **numpy.empty_like() **function we created two empty arrays: **fract **and **inte** with the same shape as the array **ar** and passed these two arrays to the** ‘out**‘ parameter along with our original array **ar** in the** numpy.modf()** function so that the fractional and integer parts which we will get from the **numpy.modf() **function can be stored in the **fract** and **inte **array respectively. In last, we printed our original array and its fractional part and integer part.

**Output:**

We can see in the output that the fractional and integer parts of our original array got separated.

### Deciding on Which Elements to Perform the Operation

Let’s see the changes in the output when we pass the** ‘where’ **parameter with an input array in the **numpy.modf()** function.

```
ar = np.array([11.1, 1.1, 12.3, 33.3])
maskCond = np.array([True, False, True, False])
resultF,resultI = np.modf(ar,where=maskCond)
print(resultF)
print(resultI)
```

Here we created a 1D array but this time we also created a** boolean **array** **saved it into a **maskCond** variable and passed that boolean array to the ‘**where’ **parameter with our original array **ar **into the **numpy.modf() **function. Let’s see the result.

**Output:**

### Returning Tuple from numpy.modf() Function

If you want to return a tuple of fractional and integer parts through **numpy.modf() **function then don’t pass the** ‘out’** parameter in the **numpy.modf() **function.

```
ar = np.array([125.9,227.4,56.15,67.777,11.12])
result = np.modf(ar)
print(result)
```

For this example, we haven’t passed the** ‘out’ **parameter in **numpy.modf()** function that’s why this function returned the tuple this time which contains both the arrays: **fractional **and** integer**.

**Output:**

### Passing Parameter ‘dtype’ in numpy.modf() Function

The **numpy.modf()** function always returns the fractional and integer array with the same data type as the input array. So, when we pass an input array with the integer data type then how can we change it to float or some other data type? Let’s see that.

```
ar = np.array([9, 2, 19])
fract, inte = np.modf(ar, dtype=float)
print("The Original array:", ar)
print("Fractional part of the array:", fract)
print("Integer part of the array:", inte)
```

In this example, we have created a 1D array like the above examples but here the only difference is all the elements of the array are of integer data type and not the** float** data type. So we have passed the parameter** ‘dtype’ **as** float** to change the data type of the output array to float. That’s why we got our output arrays (fractional and integer parts) of data type float.

**Output:**

## Conclusion

In this tutorial, we have discussed** numpy.modf()** function provided by **Python’s NumPy library**, its syntax, and parameters, and also explored a few examples to get an understanding of it. After reading this tutorial, we hope you can easily separate the fractional and integer parts of an array of floating-point numbers.

**Read More:**

## Reference

https://numpy.org/doc/stable/reference/generated/numpy.modf.html