|
1 | 1 | import numpy as np
|
2 | 2 | import pandas as pd
|
3 |
| -from pandas import DataFrame, MultiIndex, Index, Series, isna |
| 3 | +from pandas import DataFrame, MultiIndex, Index, Series, isna, Timestamp |
4 | 4 | from pandas.compat import lrange
|
5 | 5 | from pandas.util.testing import (
|
6 | 6 | assert_frame_equal,
|
7 | 7 | assert_produces_warning,
|
8 | 8 | assert_series_equal)
|
| 9 | +import pytest |
9 | 10 |
|
10 | 11 |
|
11 | 12 | def test_first_last_nth(df):
|
@@ -219,6 +220,47 @@ def test_nth_multi_index(three_group):
|
219 | 220 | assert_frame_equal(result, expected)
|
220 | 221 |
|
221 | 222 |
|
| 223 | +@pytest.mark.parametrize('data, expected_first, expected_last', [ |
| 224 | + ({'id': ['A'], 'time': Timestamp('2012-02-01 14:00:00', |
| 225 | + tz='US/Central')}, |
| 226 | + {'id': ['A'], 'time': Timestamp('2012-02-01 14:00:00', |
| 227 | + tz='US/Central')}, |
| 228 | + {'id': ['A'], 'time': Timestamp('2012-02-01 14:00:00', |
| 229 | + tz='US/Central')}), |
| 230 | + ({'id': ['A', 'B', 'A'], |
| 231 | + 'time': [Timestamp('2012-01-01 13:00:00', |
| 232 | + tz='America/New_York'), |
| 233 | + Timestamp('2012-02-01 14:00:00', |
| 234 | + tz='US/Central'), |
| 235 | + Timestamp('2012-03-01 12:00:00', |
| 236 | + tz='Europe/London')]}, |
| 237 | + {'id': ['A', 'B'], |
| 238 | + 'time': [Timestamp('2012-01-01 13:00:00', |
| 239 | + tz='America/New_York'), |
| 240 | + Timestamp('2012-02-01 14:00:00', |
| 241 | + tz='US/Central')]}, |
| 242 | + {'id': ['A', 'B'], |
| 243 | + 'time': [Timestamp('2012-03-01 12:00:00', |
| 244 | + tz='Europe/London'), |
| 245 | + Timestamp('2012-02-01 14:00:00', |
| 246 | + tz='US/Central')]}) |
| 247 | +]) |
| 248 | +def test_first_last_tz(data, expected_first, expected_last): |
| 249 | + # GH15884 |
| 250 | + # Test that the timezone is retained when calling first |
| 251 | + # or last on groupby with as_index=False |
| 252 | + |
| 253 | + df = DataFrame(data) |
| 254 | + |
| 255 | + result = df.groupby('id', as_index=False).first() |
| 256 | + expected = DataFrame(expected_first) |
| 257 | + assert_frame_equal(result, expected) |
| 258 | + |
| 259 | + result = df.groupby('id', as_index=False).last() |
| 260 | + expected = DataFrame(expected_last) |
| 261 | + assert_frame_equal(result, expected) |
| 262 | + |
| 263 | + |
222 | 264 | def test_nth_multi_index_as_expected():
|
223 | 265 | # PR 9090, related to issue 8979
|
224 | 266 | # test nth on MultiIndex
|
|
0 commit comments