Home > OS >  Why is the type of my new column integer instead of float?
Why is the type of my new column integer instead of float?

Time:02-01

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?

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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])
  •  Tags:  
  • Related