rounding

Round datetime64 array to nearest second

Round datetime64 array to nearest second Question: I have an array of type datetime64[ns]. Each element looks something like ‘2019-08-30T14:02:03.684000000’. How do I round the values to the nearest second such that I would obtain ‘2019-08-30T14:02:04’ in this example? I know I can truncate the values by t = t.astype(‘datetime64[s]’) but I specifically need to …

Total answers: 2

Round a series to N number of significant figures

Round a series to N number of significant figures Question: I have a dataframe of floats and I need make a function that will take a column and round all the values to N number of significant figures So the column might look something like: 123.949 23.87 1.9865 0.0129500 and if I wanted to round …

Total answers: 3

round() returns different result depending on the number of arguments

round() returns different result depending on the number of arguments Question: While using the round() function I noticed that I get two different results depending on whether I don’t explicitly choose the number of decimal places to include or choosing the number to be 0. x = 4.1 print(round(x)) print(round(x, 0)) It prints the following: …

Total answers: 4

Python Rounding Down to Custom Step

Python Rounding Down to Custom Step Question: We have a partially working code and 2 examples with different types of custom steps. The example 2 (Int) is working, while the example 1 is not, as it is rounding up instead of down. import math def step_size_to_precision(ss): return ss.find(‘1’) – 1 def format_value(val, step_size_str): precision = …

Total answers: 3

pandas rounding when converting float to integer

pandas rounding when converting float to integer Question: I’ve got a pandas DataFrame with a float (on decimal) index which I use to look up values (similar to a dictionary). As floats are not exactly the value they are supposed to be multiplied everything by 10 and converted it to integers .astype(int) before setting it …

Total answers: 4

How to round a numpy array?

How to round a numpy array? Question: I have a numpy array, something like below: data = np.array([ 1.60130719e-01, 9.93827160e-01, 3.63108206e-04]) and I want to round each element to two decimal places. How can I do so? Asked By: ajayramesh || Source Answers: Numpy provides two identical methods to do this. Either use np.round(data, 2) …

Total answers: 3

Round to significant figures in python

Round to significant figures in python Question: I want to round like this 42949672 -> 42949700, 2147483647 -> 2147480000, 4252017622 -> 4252020000 etc. I tried to use following , but only works for the first one. How can I make a more general one? thanks round(42949672, -2) Asked By: golu || Source Answers: To round …

Total answers: 6

Round down datetime to previous hour

Round down datetime to previous hour Question: How to round down datetime to previous hour? for example: print datetime.now().replace(microsecond=0) >> 2017-01-11 13:26:12.0 round down to previous hour: 2017-01-11 12:00:00.0 Asked By: DougKruger || Source Answers: Given you want to round down to the hour, you can simply replace microsecond, second and minute with zeros: print(datetime.now().replace(microsecond=0, …

Total answers: 3

round down to 2 decimal in python

round down to 2 decimal in python Question: I need to round down and it should be two decimal places. Tried the following, a = 28.266 print round(a, 2) 28.27 But the expected value is 28.26 only. Asked By: Suresh Kumar || Source Answers: Seems like you need the floor: import math math.floor(a * 100)/100.0 …

Total answers: 9

Python 3 Decimal rounding half down with ROUND_HALF_UP context

Python 3 Decimal rounding half down with ROUND_HALF_UP context Question: Can anybody explain or propose a fix for why when I round a decimal in Python 3 with the context set to round half up, it rounds 2.5 to 2, whereas in Python 2 it rounds correctly to 3: Python 3.4.3 and 3.5.2: >>> import …

Total answers: 4