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.
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
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