Question

I have trouble with using date & time calculation with pandas.

I think there are some logics that calculate duration automatically beginning with specific date & time. But still I couldn't find it.

I'd like to know how to add date & time info to 1 second duration time series data.

Before:
10
12
13
..
20
21
19
18

After:
1013-01-01 00:00:00 10
1013-01-01 00:00:01 12
1013-01-01 00:00:02 13
..
1013-10-04 12:45:40 20
1013-10-04 12:45:41 21
1013-10-04 12:45:42 19
1013-10-04 12:45:43 18

Any help would be appreciated. Thank you in advance.

Was it helpful?

Solution

The documentation gives a similar example at the beginning using date_range. If you have a Series object, you can make a DatetimeIndex starting at the appropriate time (I'm assuming 1013 was a typo for 2013), with a frequency of one second, and of the appropriate length:

>>> x = pd.Series(np.random.randint(8,24,23892344)) # make some random data
>>> when = pd.date_range(start=pd.datetime(2013,1,1),freq='S',periods=len(x))
>>> when
<class 'pandas.tseries.index.DatetimeIndex'>
[2013-01-01 00:00:00, ..., 2013-10-04 12:45:43]
Length: 23892344, Freq: S, Timezone: None

and then we can make a new series from the original data using this as the new index:

>>> x_with_time = pd.Series(x.values, index=when)
>>> x_with_time
2013-01-01 00:00:00    13
2013-01-01 00:00:01    14
2013-01-01 00:00:02    15
2013-01-01 00:00:03    22
2013-01-01 00:00:04    16
[...]
2013-10-04 12:45:41    21
2013-10-04 12:45:42    16
2013-10-04 12:45:43    15
Freq: S, Length: 23892344

OTHER TIPS

how about using python's datetime module combined with timeestamps?

from datetime import datetime

START_TIME = 1381069963.506736
secs = [10, 12, 13, 20, 21, 19, 18]
dates = [datetime.fromtimestamp(START_TIME + sec) for sec in secs]

after this, the list dates is a list of datetime objects with START_TIME + <given interval from time series data>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top