A.Abs
A.Abs

Reputation: 470

Create new date-time column pandas

Here is my df;

d = {'id':['abc','abc','abc','abc','def','def','def','ghj','ghj','ghj'],
     'Section': ['1H','2H','3H','4H','1H','2H','3H','1H','2H','3H'],
     'time':['00:00:00', '00:00:30', '00:01:00','00:01:30','00:00:00', '00:00:30', '00:01:00','00:00:00', '00:00:30', '00:01:00'],
     'A': [0.1,0.2,0.5,0.1,0.1,0.2,0.6,0.3,0.1,0.1],
     'B': [0.6,0.3,0.1,0.1,0.3,0.1,0.5,0.1,0.7,0.2]}

df = pd.DataFrame(d)

I would like to create a new time column of period 30 seconds for each id. I tried the following but, I don't know how to convert the output to a column in df.

g = df.groupby('id')
tp = []
for idd, group in g:
    tp.append(pd.timedelta_range('0 days 0 hours 0 minutes',periods=len(group), freq='30S'))

Desired output

    A   B   Section id     tp
0   0.1 0.6   1H    abc 00:00:00
1   0.2 0.3   2H    abc 00:00:30
2   0.5 0.1   3H    abc 00:01:00
3   0.1 0.1   4H    abc 00:01:30
4   0.1 0.3   1H    def 00:00:00
5   0.2 0.1   2H    def 00:00:30
6   0.6 0.5   3H    def 00:01:00
7   0.3 0.1   1H    ghj 00:00:00
8   0.1 0.7   2H    ghj 00:00:30
9   0.1 0.2   3H    ghj 00:01:00

Any help is much appreciated.

Upvotes: 1

Views: 93

Answers (1)

jezrael
jezrael

Reputation: 862481

Use GroupBy.cumcount for counter column, convert to second timedeltas by to_timedelta with multiple by 30:

df['tp'] = pd.to_timedelta(df.groupby('id').cumcount() * 30, unit='S')
print (df)
    id Section      time    A    B       tp
0  abc      1H  00:00:00  0.1  0.6 00:00:00
1  abc      2H  00:00:30  0.2  0.3 00:00:30
2  abc      3H  00:01:00  0.5  0.1 00:01:00
3  abc      4H  00:01:30  0.1  0.1 00:01:30
4  def      1H  00:00:00  0.1  0.3 00:00:00
5  def      2H  00:00:30  0.2  0.1 00:00:30
6  def      3H  00:01:00  0.6  0.5 00:01:00
7  ghj      1H  00:00:00  0.3  0.1 00:00:00
8  ghj      2H  00:00:30  0.1  0.7 00:00:30
9  ghj      3H  00:01:00  0.1  0.2 00:01:00

Upvotes: 1

Related Questions