[PYTHON] A method to check the maximum number of digits after the decimal point when entering pd.Series containing a numerical value

Background

When interpolating with the interpolate method of pandas.Series, the number of digits after the decimal point may increase.

Example:

python


import pandas as pd

#Data with one decimal place
series = pd.Series([0.2, np.nan, 0.5])

#Interpolation results in 2 digits
series.interpolate() # => [0.20, 0.35, 0.50]

Therefore, I decided to record the number of digits after the decimal point before interpolation and round to that number after interpolation.

In the process, I created "a method to find out how many digits after the decimal point when you enter pd.Series with a number", so I would like to share it in this article.

Created method

python


#Find out how many digits after the decimal point
def calc_n_digit_after_the_decimal_point(series):
    
    #Convert to string
    str_series = series.map(lambda x: str(x))
    
    #Extract those with a small number or less
    # 「 x[-2:] != '.0'In "," 1.It is judged whether the value is an integer though it is a float type like "0".
    mask = str_series.map(lambda x: ('.' in x) & (x[-2:] != '.0'))
    only_decimal_str_series = str_series[mask]

    #If there are a few
    if only_decimal_str_series.size > 0:
        
        #Get the maximum number of digits after a decimal
        #Round to this digit
        n_digit = only_decimal_str_series.map(lambda x: len(x.split('.')[1])).value_counts().sort_index().index.max()

    #If there are no decimals
    else:
        n_digit = 0

    return n_digit

Example of use

python


#Data with one decimal place
series = pd.Series([0.2, np.nan, 0.5])

#Record the number of digits
n_digit = calc_n_digit_after_the_decimal_point(series)

#interpolation
series = series.interpolate()

#Round to the number of recorded digits
series.round(n_digit) # => [0.2, 0.4, 0.5]

Recommended Posts

A method to check the maximum number of digits after the decimal point when entering pd.Series containing a numerical value
Check the in-memory bytes of a floating point number float in Python
Change the decimal point of logging from, to.
Display n digits after the decimal point in python
How to get the number of digits in Python
When a character string of a certain series is in the Key of the dictionary, the character string is converted to the Value of the dictionary.
Find the index of the maximum value (minimum value) of a multidimensional array
I made a function to check the model of DCGAN
Numerical approximation method when the calculation of the derivative is troublesome
Get the number of digits
When incrementing the value of a key that does not exist
How to check the memory size of a variable in Python
How to find the memory address of a Pandas dataframe value
A command to easily check the speed of the network on the console
4 methods to count the number of occurrences of integers in a certain interval (including imos method) [Python implementation]
I want to solve the problem of memory leak when outputting a large number of images with Matplotlib