Skip to content

Commit 0eaefb1

Browse files
committed
more tests
1 parent 1597220 commit 0eaefb1

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

pandas/core/arrays/datetimes.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@
3333
from pandas.tseries.offsets import Day, Tick
3434

3535
_midnight = time(0, 0)
36+
_i8_message = (
37+
"Passing integer-dtype data and a timezone to DatetimeIndex.\n"
38+
"Integer values will be interpreted differently in a future version \n"
39+
"of pandas. Previously, these were viewed as datetime64[ns] values, \n"
40+
"representing the wall time *in the specified timezone*. \n"
41+
"In the future, these will be viewed as datetime64[ns] values \n"
42+
"representing the wall time *in UTC*. This is similar to a \n"
43+
"nanosecond-precision UNIX epoch.\n\n"
44+
"To accept the future behavior, use\n\n"
45+
"\tpd.to_datetime(integer_data, utc=True).tz_convert(tz) "
46+
"\n\n"
47+
"To keep the previous behavior, use \n\n"
48+
"\tpd.to_datetime(integer_data).tz_localize(tz)"
49+
)
3650

3751

3852
def tz_to_dtype(tz):
@@ -1707,6 +1721,9 @@ def sequence_to_dt64ns(data, dtype=None, copy=False,
17071721
data, inferred_tz = objects_to_datetime64ns(
17081722
data, dayfirst=dayfirst, yearfirst=yearfirst)
17091723
tz = maybe_infer_tz(tz, inferred_tz)
1724+
# When a sequence of timestamp objects is passed, we always
1725+
# want to treat the (now i8-valued) data as UTC timestamps,
1726+
# not wall times.
17101727
int_as_wall_time = False
17111728

17121729
# `data` may have originally been a Categorical[datetime64[ns, tz]],
@@ -1738,8 +1755,8 @@ def sequence_to_dt64ns(data, dtype=None, copy=False,
17381755
if data.dtype != _INT64_DTYPE:
17391756
data = data.astype(np.int64, copy=False)
17401757
if int_as_wall_time and tz is not None:
1741-
warnings.warn("test", FutureWarning)
17421758
tz = timezones.maybe_get_tz(tz)
1759+
warnings.warn(_i8_message, FutureWarning, stacklevel=4)
17431760
data = conversion.tz_localize_to_utc(data.view('i8'), tz,
17441761
ambiguous=ambiguous)
17451762
data = data.view(_NS_DTYPE)

pandas/tests/dtypes/test_common.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ def test_is_datetime64tz_dtype():
209209
assert not com.is_datetime64tz_dtype(object)
210210
assert not com.is_datetime64tz_dtype([1, 2, 3])
211211
assert not com.is_datetime64tz_dtype(pd.DatetimeIndex([1, 2, 3]))
212-
assert com.is_datetime64tz_dtype(pd.DatetimeIndex(
213-
[1, 2, 3], tz="US/Eastern"))
212+
assert com.is_datetime64tz_dtype(pd.DatetimeIndex(['2000'],
213+
tz="US/Eastern"))
214214

215215

216216
def test_is_timedelta64_dtype():
@@ -286,7 +286,7 @@ def test_is_datetimelike():
286286
assert com.is_datetimelike(pd.PeriodIndex([], freq="A"))
287287
assert com.is_datetimelike(np.array([], dtype=np.datetime64))
288288
assert com.is_datetimelike(pd.Series([], dtype="timedelta64[ns]"))
289-
assert com.is_datetimelike(pd.DatetimeIndex([1, 2, 3], tz="US/Eastern"))
289+
assert com.is_datetimelike(pd.DatetimeIndex(["2000"], tz="US/Eastern"))
290290

291291
dtype = DatetimeTZDtype("ns", tz="US/Eastern")
292292
s = pd.Series([], dtype=dtype)
@@ -480,7 +480,7 @@ def test_needs_i8_conversion():
480480
assert com.needs_i8_conversion(np.datetime64)
481481
assert com.needs_i8_conversion(pd.Series([], dtype="timedelta64[ns]"))
482482
assert com.needs_i8_conversion(pd.DatetimeIndex(
483-
[1, 2, 3], tz="US/Eastern"))
483+
["2000"], tz="US/Eastern"))
484484

485485

486486
def test_is_numeric_dtype():
@@ -541,7 +541,7 @@ def test_is_extension_type(check_scipy):
541541
assert com.is_extension_type(pd.Series(cat))
542542
assert com.is_extension_type(pd.SparseArray([1, 2, 3]))
543543
assert com.is_extension_type(pd.SparseSeries([1, 2, 3]))
544-
assert com.is_extension_type(pd.DatetimeIndex([1, 2, 3], tz="US/Eastern"))
544+
assert com.is_extension_type(pd.DatetimeIndex(['2000'], tz="US/Eastern"))
545545

546546
dtype = DatetimeTZDtype("ns", tz="US/Eastern")
547547
s = pd.Series([], dtype=dtype)
@@ -635,8 +635,8 @@ def test__get_dtype_fails(input_param):
635635
(pd.DatetimeIndex([1, 2]), np.datetime64),
636636
(pd.DatetimeIndex([1, 2]).dtype, np.datetime64),
637637
('<M8[ns]', np.datetime64),
638-
(pd.DatetimeIndex([1, 2], tz='Europe/London'), pd.Timestamp),
639-
(pd.DatetimeIndex([1, 2], tz='Europe/London').dtype,
638+
(pd.DatetimeIndex(['2000'], tz='Europe/London'), pd.Timestamp),
639+
(pd.DatetimeIndex(['2000'], tz='Europe/London').dtype,
640640
pd.Timestamp),
641641
('datetime64[ns, Europe/London]', pd.Timestamp),
642642
(pd.SparseSeries([1, 2], dtype='int32'), np.int32),

pandas/tests/indexes/datetimes/test_astype.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,12 @@ def test_integer_index_astype_datetime(self, tz, dtype):
246246
else:
247247
ex_warn = None
248248

249-
with tm.assert_produces_warning(ex_warn, check_stacklevel=False):
249+
with tm.assert_produces_warning(ex_warn):
250250
# XXX: This likely shouldn't warn.
251251
# This raised on 0.24.x, so we can probably do the right thing
252252
# now.
253253
result = pd.Index(val).astype(dtype)
254-
with tm.assert_produces_warning(ex_warn, check_stacklevel=False):
254+
with tm.assert_produces_warning(ex_warn):
255255
expected = pd.DatetimeIndex(val, tz=tz)
256256
tm.assert_index_equal(result, expected)
257257

pandas/tests/indexes/datetimes/test_construction.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,13 @@ def test_range_kwargs_deprecated(self):
380380
with tm.assert_produces_warning(FutureWarning):
381381
DatetimeIndex(start='1/1/2000', end='1/10/2000', freq='D')
382382

383+
def test_integer_values_and_tz_deprecated(self):
384+
values = np.array([946684800000000000])
385+
with tm.assert_produces_warning(FutureWarning):
386+
result = DatetimeIndex(values, tz='US/Central')
387+
expected = pd.DatetimeIndex(['2000-01-01T00:00:00'], tz="US/Central")
388+
tm.assert_index_equal(result, expected)
389+
383390
def test_constructor_coverage(self):
384391
rng = date_range('1/1/2000', periods=10.5)
385392
exp = date_range('1/1/2000', periods=10)

0 commit comments

Comments
 (0)