Pandas Is it possible to add new time values with empty values in columns in a csv with a time sequence?

Question:

I have a csv file that looks something like this

Time OI V
10:00:23 5.4 27
10:00:24 -0.7 1
10:00:28 -0.5 4
10:00:29 0.2 12

Can I somehow add new time values using Pandas while filling the columns with zeros or Nan? For the entire csv file.

What would have turned out something like that ?

Time OI V
10:00:23 5.4 27
10:00:24 -0.7 1
10:00:25 0 Nan
10:00:26 0 Nan
10:00:27 0 Nan
10:00:28 -0.5 4
10:00:29 0.2 12
Asked By: Show_man

||

Answers:

Convert column to datetimes, create DatetimeIndex and add missing values by DataFrame.asfreq, last replace NaNs in OI column:

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

df = df.set_index('Time').asfreq('S').fillna({'OI':0})
df.index = df.index.time
print (df)
           OI     V
10:00:23  5.4  27.0
10:00:24 -0.7   1.0
10:00:25  0.0   NaN
10:00:26  0.0   NaN
10:00:27  0.0   NaN
10:00:28 -0.5   4.0
10:00:29  0.2  12.0

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

df = df.set_index('Time').asfreq('S').fillna({'OI':0}).reset_index()
df['Time'] = df['Time'].dt.time
print (df)
       Time   OI     V
0  10:00:23  5.4  27.0
1  10:00:24 -0.7   1.0
2  10:00:25  0.0   NaN
3  10:00:26  0.0   NaN
4  10:00:27  0.0   NaN
5  10:00:28 -0.5   4.0
6  10:00:29  0.2  12.0
Answered By: jezrael
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.