Skip to content

Commit 6a173d7

Browse files
committed
added all the test for the pivot hourly
now the functionality is there, if OK, we could improve the docstrings
1 parent fcde169 commit 6a173d7

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

pandas/tseries/pivot.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def last_col2front(df, col_no=1):
139139

140140

141141
def extended_info(df, time_cols=True, aggreg=True, aggreg_func=None,
142-
datetime_index=False):
142+
):
143143
"""add extended information to a timeseries pivot
144144
"""
145145

@@ -156,18 +156,19 @@ def extended_info(df, time_cols=True, aggreg=True, aggreg_func=None,
156156
df_extended['sum'] = df_extended[cols].sum(1)
157157
df_extended['min'] = df_extended[cols].min(1)
158158
df_extended['max'] = df_extended[cols].max(1)
159-
df_extended['max'] = df_extended[cols].std(1)
159+
df_extended['std'] = df_extended[cols].std(1)
160+
#TODO: how to add more functions in flexible way? check other pandas functions
161+
if aggreg_func:
162+
df_extended['aggregated'] = df_extended[cols].aggreg_func(1)
160163

161164
#add some metadata
162165
#TODO: add function to make index a datetime with the argument above using the rng below
163166
#TODO: convert the range to lower frequencies and reuse the function.
164167
rng = default_rng()
165168
df_extended['doy'] = rng.dayofyear
166-
# df_extended = last_col2front(df_extended)
167169
df_extended['month'] = rng.month
168-
# df_extended = last_col2front(df_extended)
169170
df_extended['day'] = rng.day
170-
# df_extended = last_col2front(df_extended)
171+
#add 1 to have hours formatted in "natural" and not programming counting
171172
df_extended['hour'] = rng.hour + 1
172173
df_extended = last_col2front(df_extended, col_no=4)
173174

pandas/tseries/tests/test_util.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,21 @@ def get_mdh(DatetimeIndex, index):
6161
df94_noleap = df94[(df94.index.month == 2) & (df94.index.day == 29)]
6262
np.testing.assert_equal(df94_noleap.values, nan_arr)
6363
### extended functionaliy
64+
ext = pivot.extended_info(annual)
65+
## descriptive statistics
66+
#mean
67+
tm.assert_frame_equal(annual.mean(1), ext['mean'])
68+
tm.assert_frame_equal(annual.sum(1), ext['sum'])
69+
tm.assert_frame_equal(annual.min(1), ext['min'])
70+
tm.assert_frame_equal(annual.min(1), ext['max'])
71+
tm.assert_frame_equal(annual.std(1), ext['std'])
6472

73+
## additional time columns for easier filtering
74+
np.testing.assert_equal(ext['doy'].values, annual.index.dayofyear)
75+
np.testing.assert_equal(ext['day'].values, annual.index.day)
76+
#the hour is incremented by 1
77+
np.testing.assert_equal(ext['hour'].values, (annual.index.hour +1))
6578

66-
6779

6880
def test_daily(self):
6981
rng = date_range('1/1/2000', '12/31/2004', freq='D')

0 commit comments

Comments
 (0)