0

I have a dataframe where my index is an elapsed seconds series.

Depth_m | Temperature_degC | Salinity_PSU | OBS S9604_mV | OBS highsens S9604_mV | OBS S9602_mV | OBS S9603_mV | Time elapsed_sec                           

0.00    |        35.687    |    28.9931   |   36.7530    |        0.0082         |    0.0024    |    0.0059    | 0.0120
0.25    |        35.684    |    28.9932   |   36.7531    |        0.0083         |    0.0026    |    0.0060    | 0.0106
0.50    |        35.687    |    28.9931   |   36.7532    |        0.0079         |    0.0021    |    0.0055    | 0.0099
0.75    |        35.687    |    28.9931   |   36.7532    |        0.0305         |    0.0075    |    0.0056    | 0.0101

I would like to calculate create a new series obtained from a start time and elapsed seconds. I am using python v 2.7 with pandas. Do any of you know how to obtain that? Thanks

trincot
  • 317,000
  • 35
  • 244
  • 286

2 Answers2

1

Something like this?

start_time = pd.Timestamp('2016-1-1 00:00')
df = pd.DataFrame({'seconds': [ 1, 2, 3]})
df['new_time'] = [start_time + dt.timedelta(seconds=s) for s in df.seconds]

>>> df
   seconds            new_time
0        1 2016-01-01 00:00:01
1        2 2016-01-01 00:00:02
2        3 2016-01-01 00:00:03
Alexander
  • 105,104
  • 32
  • 201
  • 196
0

That should do the trick

from __future__ import print_function, division
import pandas as pd

start_time = 14
data = pd.read_csv('data.txt', sep="|", header=0,   skip_blank_lines=True)
data['Time'] = pd.Series(data[' Time elapsed_sec'] + start_time, index=data.index)
print(data)

Missing is the conversion to datetime like Convert Pandas Column to DateTime

Lageos
  • 183
  • 1
  • 9
  • Thanks a lot Lageos, I tried your solution at first and then realised I first needed to use the time elapsed column as a series and not as my index. – user5946875 Feb 21 '16 at 07:33
  • Now I get a TypeError: unsupported operand type(s) for +: 'float' and 'datetime.datetime'. It might be because the series are fractions of a second? – user5946875 Feb 21 '16 at 07:44
  • Perhaps it is necessary to convert with `... pd.Series(float(data[' Time elapsed_sec'] ) + start_time...`. – Lageos Feb 24 '16 at 21:37
  • the float conversion didn't do the trick although I've managed to convert to a datetime format (%S) but another error occurs, it says "can only operate on a datetimes for subtraction, but the operator [__add__] was passed" ... any other ideas? – user5946875 Mar 09 '16 at 11:54