You are performing the rounding operation passing a series as an argument. Instead you need to fix this to perform the rounding up for each value in the series. I suggest you use map with a lambda in the function to do it:

Data['Numerator'] = Data['Numerator'].map(lambda x: Decimal(x).quantize(Decimal('.1'), rounding=ROUND_HALF_UP))

The output we get is as expected:

  Code Disaggregation Numerator
0    x              a      19.3
1    x              b      82.1
2    x          Total     101.2
Answer from Celius Stingher on Stack Overflow
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.round.html
pandas.DataFrame.round — pandas 3.0.2 documentation
For values exactly halfway between rounded decimal values, pandas rounds to the nearest even value (e.g.
Discussions

python - Round up half of the hour in pandas - Stack Overflow
round() function in pandas rounds down the time 07:30 to 07:00 But I want to round up any time which passes the 30 minutes (inclusive). Eg. 07:15 to 07:00 05:25 to 05:00 22:30 to 23:00 18:45 to 19:... More on stackoverflow.com
🌐 stackoverflow.com
[pandas] How to round up to the nearest 0.5?
import numpy as np df['volume_round_up'] = np.ceil(df['volume'] * 2) / 2 df['map'] = df['volume_round_up'].map(di) or in one shot: df['map'] = (np.ceil(df['volume'] * 2) / 2).map(di) More on reddit.com
🌐 r/learnpython
10
11
January 5, 2022
python - Python3 pandas dataframe round .5 always up - Stack Overflow
I want to round values in my pandas.DataFrame such that 0.5 is always rounded up. A way to fix it would be to use the decimal module with Decimal datatype as described here: How to properly round up half float numbers in Python? More on stackoverflow.com
🌐 stackoverflow.com
dataframe - Rounding Python Values to the Nearest Half Number - Stack Overflow
I'm struggling with a few things right now using python dataframes. I have one column with a long list of numbers ranging from 1-5. For example, the column contains the values (3.6, 2.1, 4.7, 3.8, ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Real Python
realpython.com › python-rounding
How to Round Numbers in Python – Real Python
December 7, 2024 - Python’s built-in round() function uses the rounding half to even strategy, which rounds numbers like 2.5 to 2 and 3.5 to 4. This method helps minimize rounding bias in datasets. To round numbers to specific decimal places, you can use the round() function with a second argument specifying the number of decimals. For more advanced rounding strategies, you can explore Python’s decimal module or use NumPy and pandas for data science applications.
Top answer
1 of 2
2

timestamps

You need to use dt.round. This is however a bit as the previous/next hour behavior depends on the hour itself. You can force it by adding or subtracting a small amount of time (here 1ns):

s = pd.to_datetime(pd.Series(['1/2/2021 3:45', '25/4/2021 12:30', 
                              '25/4/2021 13:30', '12/4/2022 23:45']))

# xx:30 -> rounding depending on the hour parity (default)
s.dt.round(freq='1h')

0   2021-01-02 04:00:00
1   2021-04-25 12:00:00    <- -30min
2   2021-04-25 14:00:00    <- +30min
3   2022-12-05 00:00:00
dtype: datetime64[ns]


# 00:30 -> 00:00 (force down)
s.sub(pd.Timedelta('1ns')).dt.round(freq='1h')

0   2021-01-02 04:00:00
1   2021-04-25 12:00:00
2   2021-04-25 13:00:00
3   2022-12-05 00:00:00
dtype: datetime64[ns]


# 00:30 -> 01:00 (force up)
s.add(pd.Timedelta('1ns')).dt.round(freq='1h')

0   2021-01-02 04:00:00
1   2021-04-25 12:00:00
2   2021-04-25 13:00:00
3   2022-12-05 00:00:00
dtype: datetime64[ns]

floats

IIUC, you can use divmod (or numpy.modf) to get the integer and decimal part, then perform simple boolean arithmetic:

s = pd.Series([7.15, 5.25, 22.30, 18.45])

s2, r = s.divmod(1)  # or np.modf(s)

s2[r.ge(0.3)] += 1

s2 = s2.astype(int)

Alternative: using mod and boolean to int equivalence:

s2 = s.astype(int)+s.mod(1).ge(0.3)

output:

0     7
1     5
2    23
3    19
dtype: int64

Note on precision. It is not always easy to compare floats due to floating point arithmetics. For instance using gt would fail on the 22.30 here. To ensure precision round to 2 digits first.

s.mod(1).round(2).ge(0.3)

or use integers:

s.mod(1).mul(100).astype(int).ge(30)
2 of 2
1

Here a version that works with timestamps:

#dummy data:
df = pd.DataFrame({'time':pd.to_datetime([np.random.randint(0,10**8) for a in range(10)], unit='s')})


def custom_round(df, col, out):
    if df[col].minute >= 30:
        df[out] = df[col].ceil('H')
    else:
        df[out] = df[col].floor('H')
    return df


df.apply(lambda x: custom_round(x, 'time', 'new_time'), axis=1)

#edit:

using numpy:

def custom_round(df, col, out):
    df[out] = np.where(
        (
            df['time'].dt.minute>=30), 
            df[col].dt.ceil('H'), 
            df[col].dt.floor('H')
    )
    return df
df = custom_round(df, 'time', 'new_time')
🌐
Data to Fish
datatofish.com › round-values-pandas-dataframe
How to Round Values in a pandas DataFrame
You can also round all numerical values in a DataFrame at once: import pandas as pd data = {'fish': ['salmon', 'pufferfish', 'shark'], 'length_m': [1.523, 0.2165, 2.1], 'width_cm': [10.2, 3.14159, 90.0] } df = pd.DataFrame(data) df = df.round(2) print(df)
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.round.html
pandas.Series.round — pandas 3.0.2 documentation - PyData |
For values exactly halfway between rounded decimal values, pandas rounds to the nearest even value (e.g.
🌐
Note.nkmk.me
note.nkmk.me › home › python › pandas
pandas: Round, floor, and ceil for DataFrame and Series | note.nkmk.me
January 15, 2024 - As of version 2.1, pandas does not provide methods for standard rounding (rounding half up) and rounding down and up decimals (floor and ceiling).
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › [pandas] how to round up to the nearest 0.5?
r/learnpython on Reddit: [pandas] How to round up to the nearest 0.5?
January 5, 2022 -

I have a dataframe df:

df = pd.DataFrame({"volume": [0.3300, 5.600, 64.0915, 1.730000, 4.123000]})
volume
0.3300
5.600
64.0915
1.730000
4.123000

I also have a non-exhausting dict di:

di = {
    0.5: 6.26,
    1.0: 6.28,
    1.5: 6.36,
    2.0: 6.46,
    2.5: 6.56,
    3.0: 6.66,
    3.5: 6.76,
    4.0: 6.86,
    4.5: 6.96,
    5.0: 6.98,
    5.5: 7.15
    ...
}

I need to create a new column ["map"] where I map di to df["volume"].

df["map"] = df["volume"].map(di)

but for that I need to round up each number in df["volume"] to the next 0.5, so the values should look like:

volume volume_round_up
0.3300 0.5
5.600 6.0
64.0915 64.5
1.730000 2.0
4.123000 4.5

How can I do this in a vectorized way?

🌐
Saturn Cloud
saturncloud.io › blog › the-right-way-to-round-pandasdataframe
The Right Way to Round pandasDataFrame | Saturn Cloud Blog
August 25, 2023 - There are different ways to round ... The most common rounding conventions are: Round half up: If the digit to the right of the rounding digit is 5 or greater, round up; otherwise, round down....
🌐
freeCodeCamp
freecodecamp.org › news › how-to-round-a-float-in-pandas
Pandas round() Method – How To Round a Float in Pandas
March 13, 2023 - The number of decimal places to be returned is passed in as a parameter. round(2) return rounds a number to two decimal places. ... import pandas as pd data = {'cost':[20.5550, 21.03535, 19.67373, 18.233233]} df = pd.DataFrame(data) df['rounded_cost'] = df['cost'].round(2) print(df)
🌐
Trymito
trymito.io › excel-to-python › functions › math › ROUND
Excel to Python: ROUND Function - A Complete Guide | Mito
By default, pandas uses the 'round half to even' method, often referred to as 'bankers' rounding'. This might differ from the expected behavior in Excel which rounds 0.5 always up.
🌐
Educative
educative.io › answers › how-to-round-up-a-dataframe-in-pandas
How to round up a DataFrame in pandas
The round() function in pandas is used to round up a DataFrame to a specified number of decimal places.
🌐
Pandas
pandas.pydata.org › docs › dev › reference › api › pandas.DataFrame.round.html
pandas.DataFrame.round — pandas documentation
For values exactly halfway between rounded decimal values, pandas rounds to the nearest even value (e.g.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.dt.round.html
pandas.Series.dt.round — pandas 3.0.1 documentation
Perform round operation on the data to the specified freq · The frequency level to round the index to. Must be a fixed frequency like ‘s’ (second) not ‘ME’ (month end). See frequency aliases for a list of possible freq values