Skip to content

Commit e2cbe76

Browse files
author
Chang She
committed
ENH: pivot annual hourly #2183
1 parent 35dd6ac commit e2cbe76

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

pandas/tseries/tests/test_util.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import unittest
33

44
import numpy as np
5+
from numpy.testing.decorators import slow
56

67
from pandas import Series, date_range
78
import pandas.util.testing as tm
@@ -36,6 +37,34 @@ def test_daily(self):
3637
leaps.index = leaps.index.year
3738
tm.assert_series_equal(annual[day].dropna(), leaps)
3839

40+
@slow
41+
def test_hourly(self):
42+
rng_hourly = date_range('1/1/1994', periods=(18* 8760 + 4*24), freq='H')
43+
data_hourly = np.random.randint(100, 350, rng_hourly.size)
44+
data_hourly = data_hourly.astype('float64')
45+
ts_hourly = Series(data_hourly, index=rng_hourly)
46+
47+
grouped = ts_hourly.groupby(ts_hourly.index.year)
48+
hoy = grouped.apply(lambda x: x.reset_index(drop=True))
49+
hoy = hoy.index.droplevel(0).values
50+
hoy[-isleapyear(ts_hourly.index.year) & (hoy >= 1416)] += 24
51+
hoy += 1
52+
53+
annual = pivot_annual(ts_hourly)
54+
55+
for i in [1, 1416, 1417, 1418, 8784]:
56+
subset = ts_hourly[hoy == i]
57+
subset.index = [x.year for x in subset.index]
58+
59+
tm.assert_series_equal(annual[i].dropna(), subset)
60+
61+
leaps = ts_hourly[(ts_hourly.index.month == 2) &
62+
(ts_hourly.index.day == 29) &
63+
(ts_hourly.index.hour == 0)]
64+
hour = leaps.index.dayofyear[0] * 24 - 23
65+
leaps.index = leaps.index.year
66+
tm.assert_series_equal(annual[hour].dropna(), leaps)
67+
3968
def test_weekly(self):
4069
pass
4170

@@ -46,7 +75,6 @@ def test_monthly(self):
4675
annual = pivot_annual(ts, 'M')
4776

4877
month = ts.index.month
49-
5078
for i in range(1, 13):
5179
subset = ts[month == i]
5280
subset.index = [x.year for x in subset.index]

pandas/tseries/util.py

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import numpy as np
22

3+
import pandas as pd
4+
35
from pandas.core.frame import DataFrame
46
import pandas.core.nanops as nanops
57

@@ -56,6 +58,14 @@ def pivot_annual(series, freq=None):
5658
width = 12
5759
offset = index.month - 1
5860
columns = range(1, 13)
61+
elif freq == 'H':
62+
width = 8784
63+
grouped = series.groupby(series.index.year)
64+
defaulted = grouped.apply(lambda x: x.reset_index(drop=True))
65+
defaulted.index = defaulted.index.droplevel(0)
66+
offset = np.asarray(defaulted.index)
67+
offset[-isleapyear(year) & (offset >= 1416)] += 24
68+
columns = range(1, 8785)
5969
else:
6070
raise NotImplementedError(freq)
6171

0 commit comments

Comments
 (0)