|
3 | 3 | import numpy as np
|
4 | 4 | from numpy.testing import assert_equal
|
5 | 5 |
|
6 |
| -import pandas |
| 6 | +import pandas as pd |
7 | 7 | from pandas import DataFrame, Series, Index, MultiIndex, Grouper
|
8 | 8 | from pandas.tools.merge import concat
|
9 | 9 | from pandas.tools.pivot import pivot_table, crosstab
|
@@ -181,6 +181,42 @@ def test_pivot_index_with_nan(self):
|
181 | 181 | columns = Index(['C1','C2','C3','C4'],name='b'))
|
182 | 182 | tm.assert_frame_equal(result, expected)
|
183 | 183 |
|
| 184 | + def test_pivot_with_tz(self): |
| 185 | + # GH 5878 |
| 186 | + df = DataFrame({'dt1': [datetime.datetime(2013, 1, 1, 9, 0), |
| 187 | + datetime.datetime(2013, 1, 2, 9, 0), |
| 188 | + datetime.datetime(2013, 1, 1, 9, 0), |
| 189 | + datetime.datetime(2013, 1, 2, 9, 0)], |
| 190 | + 'dt2': [datetime.datetime(2014, 1, 1, 9, 0), |
| 191 | + datetime.datetime(2014, 1, 1, 9, 0), |
| 192 | + datetime.datetime(2014, 1, 2, 9, 0), |
| 193 | + datetime.datetime(2014, 1, 2, 9, 0)], |
| 194 | + 'data1': range(4), 'data2': range(4)}) |
| 195 | + |
| 196 | + df['dt1'] = df['dt1'].apply(lambda d: pd.Timestamp(d, tz='US/Pacific')) |
| 197 | + df['dt2'] = df['dt2'].apply(lambda d: pd.Timestamp(d, tz='Asia/Tokyo')) |
| 198 | + |
| 199 | + exp_col1 = Index(['data1', 'data1', 'data2', 'data2']) |
| 200 | + exp_col2 = pd.DatetimeIndex(['2014/01/01 09:00', '2014/01/02 09:00'] * 2, |
| 201 | + name='dt2', tz='Asia/Tokyo') |
| 202 | + exp_col = pd.MultiIndex.from_arrays([exp_col1, exp_col2]) |
| 203 | + expected = DataFrame([[0, 2, 0, 2], [1, 3, 1, 3]], |
| 204 | + index=pd.DatetimeIndex(['2013/01/01 09:00', '2013/01/02 09:00'], |
| 205 | + name='dt1', tz='US/Pacific'), |
| 206 | + columns=exp_col) |
| 207 | + |
| 208 | + pv = df.pivot(index='dt1', columns='dt2') |
| 209 | + tm.assert_frame_equal(pv, expected) |
| 210 | + |
| 211 | + expected = DataFrame([[0, 2], [1, 3]], |
| 212 | + index=pd.DatetimeIndex(['2013/01/01 09:00', '2013/01/02 09:00'], |
| 213 | + name='dt1', tz='US/Pacific'), |
| 214 | + columns=pd.DatetimeIndex(['2014/01/01 09:00', '2014/01/02 09:00'], |
| 215 | + name='dt2', tz='Asia/Tokyo')) |
| 216 | + |
| 217 | + pv = df.pivot(index='dt1', columns='dt2', values='data1') |
| 218 | + tm.assert_frame_equal(pv, expected) |
| 219 | + |
184 | 220 | def test_margins(self):
|
185 | 221 | def _check_output(res, col, index=['A', 'B'], columns=['C']):
|
186 | 222 | cmarg = res['All'][:-1]
|
@@ -235,7 +271,7 @@ def test_pivot_integer_columns(self):
|
235 | 271 | d = datetime.date.min
|
236 | 272 | data = list(product(['foo', 'bar'], ['A', 'B', 'C'], ['x1', 'x2'],
|
237 | 273 | [d + datetime.timedelta(i) for i in range(20)], [1.0]))
|
238 |
| - df = pandas.DataFrame(data) |
| 274 | + df = DataFrame(data) |
239 | 275 | table = df.pivot_table(values=4, index=[0, 1, 3], columns=[2])
|
240 | 276 |
|
241 | 277 | df2 = df.rename(columns=str)
|
@@ -286,7 +322,7 @@ def test_pivot_columns_lexsorted(self):
|
286 | 322 | iproduct = np.random.randint(0, len(products), n)
|
287 | 323 | items['Index'] = products['Index'][iproduct]
|
288 | 324 | items['Symbol'] = products['Symbol'][iproduct]
|
289 |
| - dr = pandas.date_range(datetime.date(2000, 1, 1), datetime.date(2010, 12, 31)) |
| 325 | + dr = pd.date_range(datetime.date(2000, 1, 1), datetime.date(2010, 12, 31)) |
290 | 326 | dates = dr[np.random.randint(0, len(dr), n)]
|
291 | 327 | items['Year'] = dates.year
|
292 | 328 | items['Month'] = dates.month
|
|
0 commit comments