|
4 | 4 | from numpy.testing.utils import assert_array_equal
|
5 | 5 | from pandas.util.testing import assert_frame_equal
|
6 | 6 | import pandas as pd
|
| 7 | +from pandas.tseries.index import DatetimeIndex |
7 | 8 | import pytest
|
8 | 9 | import pytz
|
9 | 10 |
|
@@ -123,11 +124,11 @@ def test_read_all_cols_all_dtypes(tickstore_lib, chunk_size):
|
123 | 124 | # Treat missing strings as None
|
124 | 125 | data[0]['ns'] = None
|
125 | 126 | data[1]['os'] = None
|
126 |
| - # Strip TZ from the data for the moment |
127 |
| - data[0]['index'] = dt(1970, 1, 1) |
128 |
| - data[1]['index'] = dt(1970, 1, 1, 0, 0, 1) |
129 |
| - expected = pd.DataFrame(data) |
130 |
| - expected = expected.set_index('index') |
| 127 | + index = DatetimeIndex([dt(1970, 1, 1, tzinfo=mktz('UTC')), |
| 128 | + dt(1970, 1, 1, 0, 0, 1, tzinfo=mktz('UTC'))], |
| 129 | + ) |
| 130 | + index.tz = mktz() |
| 131 | + expected = pd.DataFrame(data, index=index) |
131 | 132 | expected = expected[df.columns]
|
132 | 133 | assert_frame_equal(expected, df, check_names=False)
|
133 | 134 |
|
@@ -229,6 +230,41 @@ def test_date_range_end_not_in_range(tickstore_lib):
|
229 | 230 | assert tickstore_lib._collection.find(f.call_args_list[-1][0][0]).count() == 1
|
230 | 231 |
|
231 | 232 |
|
| 233 | +@pytest.mark.parametrize('tz_name', ['UTC', |
| 234 | + 'Europe/London', # Sometimes ahead of UTC |
| 235 | + 'America/New_York', # Behind UTC |
| 236 | + ]) |
| 237 | +def test_date_range_default_timezone(tickstore_lib, tz_name): |
| 238 | + """ |
| 239 | + We assume naive datetimes are user-local |
| 240 | + """ |
| 241 | + DUMMY_DATA = [ |
| 242 | + {'a': 1., |
| 243 | + 'b': 2., |
| 244 | + 'index': dt(2013, 1, 1, tzinfo=mktz(tz_name)) |
| 245 | + }, |
| 246 | + # Half-way through the year |
| 247 | + {'b': 3., |
| 248 | + 'c': 4., |
| 249 | + 'index': dt(2013, 7, 1, tzinfo=mktz(tz_name)) |
| 250 | + }, |
| 251 | + ] |
| 252 | + |
| 253 | + with patch('arctic.date._mktz.DEFAULT_TIME_ZONE_NAME', tz_name): |
| 254 | + tickstore_lib.chunk_size = 1 |
| 255 | + tickstore_lib.write('SYM', DUMMY_DATA) |
| 256 | + df = tickstore_lib.read('SYM', date_range=DateRange(20130101, 20130701), columns=None) |
| 257 | + assert len(df) == 2 |
| 258 | + assert df.index[1] == dt(2013, 7, 1, tzinfo=mktz(tz_name)) |
| 259 | + assert df.index.tz == mktz(tz_name) |
| 260 | + |
| 261 | + df = tickstore_lib.read('SYM', date_range=DateRange(20130101, 20130101), columns=None) |
| 262 | + assert len(df) == 1 |
| 263 | + |
| 264 | + df = tickstore_lib.read('SYM', date_range=DateRange(20130701, 20130701), columns=None) |
| 265 | + assert len(df) == 1 |
| 266 | + |
| 267 | + |
232 | 268 | def test_date_range_no_bounds(tickstore_lib):
|
233 | 269 | DUMMY_DATA = [
|
234 | 270 | {'a': 1.,
|
@@ -387,31 +423,31 @@ def test_read_with_image(tickstore_lib):
|
387 | 423 | assert_array_equal(df['a'].values, np.array([37, 1, np.nan]))
|
388 | 424 | assert_array_equal(df['b'].values, np.array([np.nan, np.nan, 4]))
|
389 | 425 | assert_array_equal(df['c'].values, np.array([2, np.nan, np.nan]))
|
390 |
| - assert df.index[0] == dt(2013, 1, 1, 10) |
391 |
| - assert df.index[1] == dt(2013, 1, 1, 11) |
392 |
| - assert df.index[2] == dt(2013, 1, 1, 12) |
| 426 | + assert df.index[0] == dt(2013, 1, 1, 10, tzinfo=mktz('Europe/London')) |
| 427 | + assert df.index[1] == dt(2013, 1, 1, 11, tzinfo=mktz('Europe/London')) |
| 428 | + assert df.index[2] == dt(2013, 1, 1, 12, tzinfo=mktz('Europe/London')) |
393 | 429 |
|
394 | 430 | # Read just columns from the updates
|
395 | 431 | df = tickstore_lib.read('SYM', columns=('a', 'b'), date_range=dr, include_images=True)
|
396 | 432 | assert set(df.columns) == set(('a', 'b'))
|
397 | 433 | assert_array_equal(df['a'].values, np.array([37, 1, np.nan]))
|
398 | 434 | assert_array_equal(df['b'].values, np.array([np.nan, np.nan, 4]))
|
399 |
| - assert df.index[0] == dt(2013, 1, 1, 10) |
400 |
| - assert df.index[1] == dt(2013, 1, 1, 11) |
401 |
| - assert df.index[2] == dt(2013, 1, 1, 12) |
| 435 | + assert df.index[0] == dt(2013, 1, 1, 10, tzinfo=mktz('Europe/London')) |
| 436 | + assert df.index[1] == dt(2013, 1, 1, 11, tzinfo=mktz('Europe/London')) |
| 437 | + assert df.index[2] == dt(2013, 1, 1, 12, tzinfo=mktz('Europe/London')) |
402 | 438 |
|
403 | 439 | # Read one column from the updates
|
404 | 440 | df = tickstore_lib.read('SYM', columns=('a',), date_range=dr, include_images=True)
|
405 | 441 | assert set(df.columns) == set(('a',))
|
406 | 442 | assert_array_equal(df['a'].values, np.array([37, 1, np.nan]))
|
407 |
| - assert df.index[0] == dt(2013, 1, 1, 10) |
408 |
| - assert df.index[1] == dt(2013, 1, 1, 11) |
409 |
| - assert df.index[2] == dt(2013, 1, 1, 12) |
| 443 | + assert df.index[0] == dt(2013, 1, 1, 10, tzinfo=mktz('Europe/London')) |
| 444 | + assert df.index[1] == dt(2013, 1, 1, 11, tzinfo=mktz('Europe/London')) |
| 445 | + assert df.index[2] == dt(2013, 1, 1, 12, tzinfo=mktz('Europe/London')) |
410 | 446 |
|
411 | 447 | # Read just the image column
|
412 | 448 | df = tickstore_lib.read('SYM', columns=['c'], date_range=dr, include_images=True)
|
413 | 449 | assert set(df.columns) == set(['c'])
|
414 | 450 | assert_array_equal(df['c'].values, np.array([2, np.nan, np.nan]))
|
415 |
| - assert df.index[0] == dt(2013, 1, 1, 10) |
416 |
| - assert df.index[1] == dt(2013, 1, 1, 11) |
417 |
| - assert df.index[2] == dt(2013, 1, 1, 12) |
| 451 | + assert df.index[0] == dt(2013, 1, 1, 10, tzinfo=mktz('Europe/London')) |
| 452 | + assert df.index[1] == dt(2013, 1, 1, 11, tzinfo=mktz('Europe/London')) |
| 453 | + assert df.index[2] == dt(2013, 1, 1, 12, tzinfo=mktz('Europe/London')) |
0 commit comments