Home > OS >  PANDAS - Inserting additional columns and Values based off another column
PANDAS - Inserting additional columns and Values based off another column

Time:06-30

I have a Dataframe, with a "time in seconds" column. Which looks like the following:

   Number    Seconds      Col[2] Col[3] ... Col[n]  
0    1      57047.9293      v2     v3   ...  vn 
1    2      57048.9824      --     --        --
...
m

I am attempting to insert additional columns before the Seconds for Hours and Minutes. So it looks like so:

   Number  Hour  Min    Sec  
0     1      15    50   47.9293   ... vn
1     2      15    50   48.9824   ... vn

I am receiving the following errors with the code below

  • TypeError: unsupported type for timedelta seconds component: Series
  • TypeError: cannot convert the series to <class 'float'>

How can I resolve this properly, and is there a better way than to make multiple calls to df.insert()?

import datetime

def convertSecondsto_(time, value):

  fullTime = str(datetime.timedelta(seconds=value)).split(':')

  if time == 1 # Getting Hour Value
    value = fullTime[0]

  if time == 2 # Getting Minute Value
    value = fullTime[1]

  if time == 3 # Getting Second Value
    value == f'{float(fullTime[2]):.4f}' # only 4 Decimal places

  return value

df.insert(1, "Hour", convertSecondsto_(1, df["Seconds"])))

#TypeError: unsupported type for timedelta seconds component: Series

df.insert(1, "Hour", convertSecondsto_(1, float(df["Seconds"]))))

#TypeError: cannot convert the series to <class 'float'>

CodePudding user response:

here is one way to do it

df['hour'] = pd.to_datetime(df["Seconds"], unit='s').dt.hour
df['minute'] = pd.to_datetime(df["Seconds"], unit='s').dt.minute

    Number  Seconds     Col[2]  Col[3]  Col[n]  hour    minute
0      1    57047.9293    v2     v3      vn     15        50
1      2    57048.9824    --     --      --     15        50

CodePudding user response:

The function convertSecondsto_ takes a float value as argument. But it's receiving a series from the insert method. You need to change the function to convert every value in the series and produce a list that can be inserted in the dataframe.

def convertSecondsto_(time, value):
    convertedTime = []
    for seconds in value:
        fullTime = str(datetime.timedelta(seconds=seconds)).split(':')
        if time == 1: # Getting Hour Value
            convertedTime.append(fullTime[0])
        if time == 2: # Getting Minute Value
            convertedTime.append(fullTime[1])
        if time == 3: # Getting Second Value
            convertedTime.append(f'{float(fullTime[2]):.4f}') # only 4 Decimal places

    return convertedTime


df.insert(1, "Hour", convertSecondsto_(1, df["Seconds"]))
df.insert(2, "Min", convertSecondsto_(2, df["Seconds"]))
df.insert(3, "Sec", convertSecondsto_(3, df["Seconds"]))

This works as intended.

CodePudding user response:

you can also do something like this:

df[['hour','min','sec']] = pd.to_timedelta(df['seconds'],unit='s').astype(str).str[7:-2].str.split(':',expand=True)

>>> df
'''
      seconds hour min      sec
0  57047.9293   15  50  47.9293
1  57048.9824   15  50  48.9824
  • Related