• Post author:
  • Post category:Pandas
  • Post last modified:November 26, 2024
  • Reading time:13 mins read
You are currently viewing Pandas Get Floor or Ceil of Series

We can get the floor or ceil (Ceiling) values from the pandas Series by using series.clip(), NumPy’s floor() and ceil() functions. In simple words, the floor value is always less than or equal to the given value, and the ceiling value is always greater than or equal to the given value.

Advertisements

In this article, I will explain how we can get the floor or ceiling of a pandas series in python with several examples.

Key Points –

  • Floor and ceil functions round numerical values in a Series.
  • Use numpy.floor() for rounding down and numpy.ceil() for rounding up.
  • Original Series remains unchanged unless reassigned.
  • NaN values in the Series are retained in the output.
  • Can be easily integrated with other Pandas operations.
  • These functions can be chained with other Pandas methods for streamlined data manipulation.

Quick Examples of Floor or Ceil of a Series

If you are in a hurry, below are some quick examples of how to floor or ceiling of series in python.


# Quick examples of floor or ceil of a series

# Example 1: Get the floor values of pandas series
ser2 = np.floor(ser)

# Example 2: Get the ceil values of pandas series
ser2 = np.ceil(ser)

# Example 3: Use pandas.series.clip() function 
# To get lower values
ser2 = ser.clip(lower=0)

# Example 4: Use pandas.series.clip() function 
# To get upper values
ser2 = ser.clip(upper=0)

Initialize Pandas Series

Pandas Series is a one-dimensional, Index-labeled data structure available only in the Pandas library. It can store all the datatypes such as strings, integers, float, and other Python objects. We can access each element in the Series with the help of corresponding default indices.

Now, let’s create pandas series using a list of values.


import pandas as pd
import numpy as np

# Create the Series
ser = pd.Series([2.3,3.5,-1.3,5.7,4.8,-6.8])
print(ser)

Yields below output.


# Output:
0    2.3
1    3.5
2   -1.3
3    5.7
4    4.8
5   -6.8
dtype: float64

Get the Floor Values of the Pandas Series

Using numpy.floor() function we can get the floor of each value in the Series. floor() function takes Series as a parameter and returns the floor value of each Series element with a float data type. The floor value of the scalar x is the largest integer y, such that y<=x.


# Get the floor values of pandas series
value = np.floor(ser)
print(value)

Yields below output.


# Output:
0    2.0
1    3.0
2   -2.0
3    5.0
4    4.0
5   -7.0
dtype: float64

Get the Ceil Values of Pandas Series

Using numpy.ceil() function we can get the ceiling of each value in the Series. The ceil of the scalar x is the smallest integer i, such that i>= x. In simple words, the ceil value is always greater than equal to the given value.


# Get the ceil values of pandas series
ser2 = np.ceil(ser)
print(ser2)

Yields below output.


# Output:
0    3.0
1    4.0
2   -1.0
3    6.0
4    5.0
5   -6.0
dtype: float64

Use pandas.Series.clip() Function to Get Lower & Upper Values

Pandas Series.clip() is used to get the lower & upper values of the series. If you pass lower=0 into clip() function, it will override the least values with zero’s. If you pass upper=0 into clip() function, It will override the highest values with zeros.


# Use pandas.series.clip() function 
# To get lower values
ser2 = ser.clip(lower=0)
print(ser2)

# Output:
# 0    2.3
# 1    3.5
# 2    0.0
# 3    5.7
# 4    4.8
# 5    0.0
# dtype: float64

# Use pandas.series.clip() function 
# To get upper values
ser2 = ser.clip(upper=0)
print(ser2)

# Output:
# 0    0.0
# 1    0.0
# 2   -1.3
# 3    0.0
# 4    0.0
# 5   -6.8
# dtype: float64

Complete Example For Floor or Ceiling of a Series


import pandas as pd
import numpy as np

# Create the Series
ser = pd.Series([2.3,3.5,-1.3,5.7,4.8,-6.8])
print(ser)

# Get the floor values of pandas series
ser2 = np.floor(ser)
print(ser2)

# Get the ceil values of pandas series
ser2 = np.ceil(ser)
print(ser2)

# Use pandas.series.clip() function 
# To get lower values
ser2 = ser.clip(lower=0)
print(ser2)

# Use pandas.series.clip() function 
# To get upper values
ser2 = ser.clip(upper=0)
print(ser2)

FAQ on Pandas Get Floor or Ceil of Series

How do I get the floor of a Pandas Series?

To get the floor of each element in a Pandas Series, you can use the numpy library’s np.floor() function

How do I get the ceiling of a Pandas Series?

To get the ceiling of a Pandas Series, you can use the numpy library’s np.ceil() function.

Do I need to import any library to get the floor or ceiling of a Pandas Series?

To use floor or ceiling functions with a Pandas Series, you need to import the numpy library as it provides np.floor() and np.ceil() functions.

Can I get the floor or ceiling in-place?

The np.floor() and np.ceil() functions return a new array or Series. They don’t modify the original Series in place.

What if my Series contains NaN values?

Both np.floor() and np.ceil() handle NaN values gracefully and will keep them as NaN in the output.

Is there a built-in Pandas method for floor or ceil?

Pandas doesn’t have built-in .floor() or .ceil() methods directly in the Series or DataFrame. However, numpy functions like np.floor() and np.ceil() are the recommended way to achieve this.

Conclusion

In this article, I have explained how to floor or ceil (ceiling) of a pandas series in python using series.clip(), numpy.floor(), and numpy.ceil() functions with examples.

Happy Learning !!

References