Reputation: 23
I am working on time-series data, where my pandas dataframe has indices specified in hours, like this:
[0.0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2, 2.4, ...]
This goes on for a few thousand hours. I know that the first measurement was taken on, let's say, May 1, 2017 12:00
. How do I use this information to turn my indices into pandas datetime format?
Upvotes: 2
Views: 303
Reputation: 862641
You can add hours to index by parameter origin
in to_datetime
for DatetimeIndex
:
idx = [0.0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2, 2.4]
df = pd.DataFrame({'a':range(13)}, index=idx)
start = 'May 1, 2017 12:00'
df.index = pd.to_datetime(df.index, origin=start, unit='h')
print (df)
a
2017-05-01 12:00:00 0
2017-05-01 12:12:00 1
2017-05-01 12:24:00 2
2017-05-01 12:36:00 3
2017-05-01 12:48:00 4
2017-05-01 13:00:00 5
2017-05-01 13:12:00 6
2017-05-01 13:24:00 7
2017-05-01 13:36:00 8
2017-05-01 13:48:00 9
2017-05-01 14:00:00 10
2017-05-01 14:12:00 11
2017-05-01 14:24:00 12
Upvotes: 2
Reputation: 42916
You can use pandas.date_range
to specify the amount of periods based on the length of your index
(in this case list) and specify the frequency, which is in this case 12min
or 1/5 H
:
l = [0.0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2, 2.4]
data = {'Num':np.random.randint(1, 10, size=len(l))}
idx = pd.date_range(start=pd.Timestamp(2017, 5, 1, 12), periods=len(l), freq='12T')
df = pd.DataFrame(data = data, index= idx)
print(df)
Num
2017-05-01 12:00:00 8
2017-05-01 12:12:00 3
2017-05-01 12:24:00 3
2017-05-01 12:36:00 4
2017-05-01 12:48:00 8
2017-05-01 13:00:00 3
2017-05-01 13:12:00 6
2017-05-01 13:24:00 3
2017-05-01 13:36:00 4
2017-05-01 13:48:00 9
2017-05-01 14:00:00 5
2017-05-01 14:12:00 2
2017-05-01 14:24:00 6
Upvotes: 1