Skip to content

Commit b04455f

Browse files
Prevent OutOfBoundsDatetime error for tz-aware series
1 parent 77bc67a commit b04455f

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

Diff for: pandas/_libs/tslib.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ cdef _array_to_datetime_object(
681681
return oresult_nd, None
682682

683683

684-
def array_to_datetime_with_tz(ndarray values, tzinfo tz):
684+
def array_to_datetime_with_tz(ndarray values, tzinfo tz, unit="ns"):
685685
"""
686686
Vectorized analogue to pd.Timestamp(value, tz=tz)
687687
@@ -717,7 +717,7 @@ def array_to_datetime_with_tz(ndarray values, tzinfo tz):
717717
else:
718718
# datetime64, tznaive pydatetime, int, float
719719
ts = ts.tz_localize(tz)
720-
ts = ts.as_unit("ns")
720+
ts = ts.as_unit(unit)
721721
ival = ts._value
722722

723723
# Analogous to: result[i] = ival

Diff for: pandas/core/arrays/datetimes.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ def _from_sequence_not_strict(
336336
# DatetimeTZDtype
337337
unit = dtype.unit
338338

339-
subarr, tz, inferred_freq = _sequence_to_dt64ns(
339+
subarr, tz, inferred_freq = _sequence_to_dt64(
340340
data,
341341
copy=copy,
342342
tz=tz,
@@ -2156,7 +2156,7 @@ def std(
21562156
# Constructor Helpers
21572157

21582158

2159-
def _sequence_to_dt64ns(
2159+
def _sequence_to_dt64(
21602160
data,
21612161
*,
21622162
copy: bool = False,
@@ -2218,8 +2218,8 @@ def _sequence_to_dt64ns(
22182218
elif tz is not None and ambiguous == "raise":
22192219
# TODO: yearfirst/dayfirst/etc?
22202220
obj_data = np.asarray(data, dtype=object)
2221-
i8data = tslib.array_to_datetime_with_tz(obj_data, tz)
2222-
return i8data.view(DT64NS_DTYPE), tz, None
2221+
i8data = tslib.array_to_datetime_with_tz(obj_data, tz, out_unit)
2222+
return i8data.view(out_dtype), tz, None
22232223
else:
22242224
# data comes back here as either i8 to denote UTC timestamps
22252225
# or M8[ns] to denote wall times

Diff for: pandas/tests/series/test_constructors.py

+9
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,15 @@ def test_constructor_with_datetime_tz(self):
11481148
result = DatetimeIndex(s, freq="infer")
11491149
tm.assert_index_equal(result, dr)
11501150

1151+
def test_constructor_with_datetime_tz_ms(self):
1152+
# explicit frequency
1153+
result = Series([Timestamp("2999-01-01")], dtype="datetime64[ms, US/Pacific]")
1154+
expected = Series(
1155+
np.array(["2999-01-01"], dtype="datetime64[ms]")
1156+
).dt.tz_localize("US/Pacific")
1157+
tm.assert_series_equal(result, expected)
1158+
assert result.dtype == "datetime64[ms, US/Pacific]"
1159+
11511160
def test_constructor_with_datetime_tz4(self):
11521161
# inference
11531162
s = Series(

0 commit comments

Comments
 (0)