How to fill missing timestamps for Time column for a date in pandas

别等时光非礼了梦想. 提交于 2020-12-12 13:27:59

问题


I have a time-series data as below:

print(df)

    ric     datel       timel        val
0   xyz     2017-01-01  09:00:00     2
1   xyz     2017-01-01  09:04:00     5
2   xyz     2017-01-01  09:37:00     6

Now I have to fill missing timestamps upto 09:45:00.

Expected Output:

    ric     datel       timel        val
0   xyz     2017-01-01  09:00:00     2
1   xyz     2017-01-01  09:01:00     nan
2   xyz     2017-01-01  09:02:00     nan
3   xyz     2017-01-01  09:03:00     nan
4   xyz     2017-01-01  09:04:00     5
...
...
37  xyz     2017-01-01  09:37:00      6
...
...
45  xyz     2017-01-01  09:45:00      nan

What I tried:

df1=df.resample("1 min", on ='datel').first()

which gives output as:

              ric   datel       timel     val
datel                   
2017-01-01  xyz     2017-01-01  09:00:00    2

And also tried with pd.date_range but it mostly works with datetime column. I have two different columns date and time. Is there a way to achieve this without combining date and column into datetime?


回答1:


Main idea is use reindex by times created by date_range:

df['timel'] = pd.to_datetime(df['timel']).dt.time
start = pd.to_datetime(str(df['timel'].min()))
end = pd.to_datetime('09:45:00')
dates = pd.date_range(start=start, end=end, freq='1Min').time
#print (dates)

df = df.set_index('timel').reindex(dates).reset_index().reindex(columns=df.columns)
cols = df.columns.difference(['val'])
df[cols] = df[cols].ffill()
print (df.head())
   ric       datel     timel  val
0  xyz  2017-01-01  09:00:00  2.0
1  xyz  2017-01-01  09:01:00  NaN
2  xyz  2017-01-01  09:02:00  NaN
3  xyz  2017-01-01  09:03:00  NaN
4  xyz  2017-01-01  09:04:00  5.0

Similar solution with resample:

df['timel'] = pd.to_datetime(df['timel'])

#if missing row with 09:45:00 add it
if not (df['timel']  == pd.to_datetime('09:45:00')).any():
    df.loc[len(df.index), 'timel'] = pd.to_datetime('09:45:00')

df=df.set_index('timel').resample("1min").first().reset_index().reindex(columns=df.columns)
cols = df.columns.difference(['val'])
df[cols] = df[cols].ffill()
df['timel'] = df['timel'].dt.time
print (df.head())
   ric       datel     timel  val
0  xyz  2017-01-01  09:00:00  2.0
1  xyz  2017-01-01  09:01:00  NaN
2  xyz  2017-01-01  09:02:00  NaN
3  xyz  2017-01-01  09:03:00  NaN
4  xyz  2017-01-01  09:04:00  5.0



回答2:


After generating the date with date_range you may use a function similar to the one below to split it.

The return values can be fed into the df

from datetime import datetime

def split_datetime(date_with_time):
    """
    This function will return date and time from datetime input
    """
    date_with_time = date_with_time.split(' ')
    date = date_with_time[0]
    time = date_with_time[1].split('.')[0]
    return date, time

#Eg:                   
date, time = split_datetime(str(datetime.now()))


来源:https://stackoverflow.com/questions/49187686/how-to-fill-missing-timestamps-for-time-column-for-a-date-in-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!