How to convert one integer number of days to pandas datetime
I have a dataframe with a "time" column of floating point numbers representing days 0 to 8 and another column with other data. The time step is not continuous.
time_clean = np.arange(0, 8, 0.1)
noise = [random.random()/10 for n in range(len(time_clean))]
time = time_clean + noise
data = [random.random()*100 for n in range(len(time_clean))]
df = pd.DataFrame({"time": time, "data":data})
df.head()
data time
0 89.965240 0.041341
1 95.964621 0.109215
2 70.552763 0.232596
3 74.457244 0.330750
4 13.228426 0.471623
I want to re-display and interpolate data every 15 minutes ( 15/(60*24)
days).
I think the most efficient way to do this would be using resample
the pandas dataframes method, but for that I need to convert the time column to timestamp
and make it an index.
What's the most efficient way to do this? Can you convert int
to datetime
?
source to share
I think you need to convert the column first time
to_timedelta
and then sort_values
with resample
:
Also I think it is best to add one new row with 0
for always triggers resampling from 0
(if 0
not in a column time
it starts at minimum value time
)
df.loc[-1] = 0
df.time = pd.to_timedelta(df.time, unit='d')
df = df.sort_values('time').set_index('time').resample('15T').ffill()
print (df.head(20))
data
time
00:00:00 0.000000
00:15:00 0.000000
00:30:00 0.000000
00:45:00 0.000000
01:00:00 0.000000
01:15:00 0.000000
01:30:00 50.869889
01:45:00 50.869889
02:00:00 50.869889
02:15:00 50.869889
02:30:00 50.869889
02:45:00 50.869889
03:00:00 50.869889
03:15:00 8.846017
03:30:00 8.846017
03:45:00 8.846017
04:00:00 8.846017
04:15:00 8.846017
04:30:00 8.846017
04:45:00 8.846017
source to share