Home > front end >  Turning a variable into timedelta but in days rather than time
Turning a variable into timedelta but in days rather than time

Time:11-10

I have a dataframe where I would like to convert an integer into days. So far, I just changed it to timedelta[64]. However, it goes to to the time factor. I do not care for the time and if possible would like for it to convert to days. If time is needed to be kept, that is fine as long as it changes the days

My code:

df['Date_Numerical_Delta'] = df_1sNA['Date_Numerical'].astype('timedelta64[ns]')

Sample input:



df = pd.DataFrame({'Date_Numerical': [5, 194, 163], 'Date_Numerical_Delta': ['0 days 00:00:00.000000005', '0 days 00:00:00.000000194','0 days 00:00:00.000000163']}) 
df

Desired output:

df2 = pd.DataFrame({'Date_Numerical': [5, 194, 163], 'Date_Numerical_Delta': ['5 days', '194 days', '163 days']})
df2 

or

df3 = pd.DataFrame({'Date_Numerical':       [5, 194, 163], 
                   'Date_Numerical_Delta': ['5 days 00:00:00.000000000', '194 days 00:00:00.000000000', '163 days 00:00:00.000000000']})
df3 

CodePudding user response:

Simply done. From the comments:

df['Date_Numerical_Delta'] = pd.to_timedelta(df["Date_Numerical"], unit="D")
  • Related