|
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,64 @@ 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'], |
| 225 | + 'time': Timestamp('2012-02-01 14:00:00', |
| 226 | + tz='US/Central'), |
| 227 | + 'foo': [1]}, |
| 228 | + {'id': ['A'], |
| 229 | + 'time': Timestamp('2012-02-01 14:00:00', |
| 230 | + tz='US/Central'), |
| 231 | + 'foo': [1]}, |
| 232 | + {'id': ['A'], |
| 233 | + 'time': Timestamp('2012-02-01 14:00:00', |
| 234 | + tz='US/Central'), |
| 235 | + 'foo': [1]}), |
| 236 | + ({'id': ['A', 'B', 'A'], |
| 237 | + 'time': [Timestamp('2012-01-01 13:00:00', |
| 238 | + tz='America/New_York'), |
| 239 | + Timestamp('2012-02-01 14:00:00', |
| 240 | + tz='US/Central'), |
| 241 | + Timestamp('2012-03-01 12:00:00', |
| 242 | + tz='Europe/London')], |
| 243 | + 'foo': [1, 2, 3]}, |
| 244 | + {'id': ['A', 'B'], |
| 245 | + 'time': [Timestamp('2012-01-01 13:00:00', |
| 246 | + tz='America/New_York'), |
| 247 | + Timestamp('2012-02-01 14:00:00', |
| 248 | + tz='US/Central')], |
| 249 | + 'foo': [1, 2]}, |
| 250 | + {'id': ['A', 'B'], |
| 251 | + 'time': [Timestamp('2012-03-01 12:00:00', |
| 252 | + tz='Europe/London'), |
| 253 | + Timestamp('2012-02-01 14:00:00', |
| 254 | + tz='US/Central')], |
| 255 | + 'foo': [3, 2]}) |
| 256 | +]) |
| 257 | +def test_first_last_tz(data, expected_first, expected_last): |
| 258 | + # GH15884 |
| 259 | + # Test that the timezone is retained when calling first |
| 260 | + # or last on groupby with as_index=False |
| 261 | + |
| 262 | + df = DataFrame(data) |
| 263 | + |
| 264 | + result = df.groupby('id', as_index=False).first() |
| 265 | + expected = DataFrame(expected_first) |
| 266 | + cols = ['id', 'time', 'foo'] |
| 267 | + assert_frame_equal(result[cols], expected[cols]) |
| 268 | + |
| 269 | + result = df.groupby('id', as_index=False)['time'].first() |
| 270 | + assert_frame_equal(result, expected[['id', 'time']]) |
| 271 | + |
| 272 | + result = df.groupby('id', as_index=False).last() |
| 273 | + expected = DataFrame(expected_last) |
| 274 | + cols = ['id', 'time', 'foo'] |
| 275 | + assert_frame_equal(result[cols], expected[cols]) |
| 276 | + |
| 277 | + result = df.groupby('id', as_index=False)['time'].last() |
| 278 | + assert_frame_equal(result, expected[['id', 'time']]) |
| 279 | + |
| 280 | + |
222 | 281 | def test_nth_multi_index_as_expected():
|
223 | 282 | # PR 9090, related to issue 8979
|
224 | 283 | # test nth on MultiIndex
|
|
0 commit comments