import pandas as pd
import numpy as np
from collections import OrderedDict
# fake data
ind = pd.date_range(start="2000-01-01", freq="B", periods=10)
data = OrderedDict()
data['Date'] = ind
for h in range(1, 25):
data['h%d'%h] = np.random.randn(len(ind))
df = pd.DataFrame(data)
df = df.set_index('Date')
df = df.asfreq('D')
df
<class 'pandas.core.frame.DataFrame'> DatetimeIndex: 12 entries, 2000-01-03 00:00:00 to 2000-01-14 00:00:00 Freq: D Data columns: h1 10 non-null values h2 10 non-null values h3 10 non-null values h4 10 non-null values h5 10 non-null values h6 10 non-null values h7 10 non-null values h8 10 non-null values h9 10 non-null values h10 10 non-null values h11 10 non-null values h12 10 non-null values h13 10 non-null values h14 10 non-null values h15 10 non-null values h16 10 non-null values h17 10 non-null values h18 10 non-null values h19 10 non-null values h20 10 non-null values h21 10 non-null values h22 10 non-null values h23 10 non-null values h24 10 non-null values dtypes: float64(24)
# grab the hourly matrix
hourly_data = df.values
# generate new_index
new_ind = pd.date_range(start=df.index[0], freq="H", periods=len(df) * 24)
288
# create Series.
s = pd.Series(hourly_data.flatten(), index=new_ind)
df.ix[:5, :10]
h1 | h2 | h3 | h4 | h5 | h6 | h7 | h8 | h9 | h10 | |
---|---|---|---|---|---|---|---|---|---|---|
2000-01-03 | -0.142075 | 0.803359 | -0.009758 | -0.883526 | -0.391138 | 1.361150 | -1.164040 | -0.199118 | -0.675102 | 0.794139 |
2000-01-04 | 0.350092 | 0.953625 | 1.000110 | 1.016888 | 0.232875 | -0.886501 | -0.916722 | -0.612824 | -0.033277 | 0.593018 |
2000-01-05 | 0.050548 | -0.810534 | 0.558718 | 0.421223 | -0.041516 | -0.257585 | 1.319447 | 2.106961 | -0.627061 | 0.156575 |
2000-01-06 | 0.922721 | -0.475252 | 1.045274 | 1.073166 | 0.722628 | 1.403291 | -0.502675 | -0.673311 | -0.240045 | 1.410453 |
2000-01-07 | -0.463406 | 0.325439 | 0.463271 | 0.225835 | -0.345657 | -1.755235 | 1.307630 | -0.928235 | 0.118542 | 0.477445 |
s.head()
2000-01-03 00:00:00 -0.142075 2000-01-03 01:00:00 0.803359 2000-01-03 02:00:00 -0.009758 2000-01-03 03:00:00 -0.883526 2000-01-03 04:00:00 -0.391138 Freq: H, dtype: float64