'Why is the type of my new column integer instead of float?

Original Dataset image

I'm trying to make a new column event_t by the code below.

for i in range(len(df) - 1):
    df['event_t'][i] = df['time'][i+1] - df['time'][i]
type(df['event_t'][0]) #int64

As you can see in the image, the type of the column df['time'] is float64. But when implementing the code above, my new column 'event_t' becomes integer. How can I make my column be calculated as float, so that the decimals are alive?



Solution 1:[1]

Try this :

df['event_t'] = df.time.shift(-1)
df.event_t = df.event_t -df.time

Solution df: enter image description here

enter image description here

enter image description here

Solution 2:[2]

It looks like python is making integer-subtraction and making the result an integer. Python has the limitations for floating point sometimes. Python uses IEEE 754 doubles for its floats. I believe you have some out-of-range number beyond IEEE 754 doubles in your df, or you defined df['event_t'] incorrectly before.

You can import decimal package.

from decimal import Decimal
for i in range(len(df) - 1):
    df['event_t'][i] = Decimal(df['time'][i+1]) - Decimal(df['time'][i])

Solution 3:[3]

Use:

out = []
for i in range(len(df) - 1):
    out.append(df['time'][i+1] - df['time'][i])
out.append(0)
df['event_t'] = out

I do not know your problem exactly. Specifically, how was the df['event_t'] generated? I need more information about your problem. Meanwhile you can try the above code which uses a list to gather the new column data and then assign that on a df column.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shouhaddo Paul
Solution 2 Ka-Wa Yip
Solution 3