Pablo M.
Pablo M.

Reputation: 29

Interpolate pandas dataframe column with hours in decimal

I have a column in a dataframe with hours in decimal (0.0, 0.15, 0.30, 0.45, 1.0, 1.15, ...., 23.45) and I want to interpolate each minute instead of each 15 minutes.

I tried df.interpolate() but the result should be 0.59, 1.0, not 0.59, 0.60... Any ideas how I can solve this problem?

Upvotes: 0

Views: 221

Answers (1)

rpanai
rpanai

Reputation: 13447

With that time format it could be quite a pain to work. What comes to my mind is that you can generate another dataframe with times 1 minute apart and then merge to the left your initial dataframe.

import pandas as pd
import numpy as np

def gen_time(interval=15):
    m = 0
    h = 0
    tm = []
    while h*60 + m <= 23*60 + 45:
        tm.append(h+m*1e-2)
        m += interval
        if m == 60:
            h += 1
            m = 0
    return tm

tm = gen_time()
df = pd.DataFrame({"time":tm,
                  "values":np.random.randn(len(tm))})

ddf = pd.DataFrame({"time":gen_time(interval=1)})

df = pd.merge(ddf, df,on="time", how="left")

Upvotes: 1

Related Questions