Skip to content

Commit 8dc832f

Browse files
authored
annotation, missing test case, perf DTA.mode (#47418)
1 parent 37d4c31 commit 8dc832f

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

pandas/_libs/tslibs/tzconversion.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ cdef ndarray[int64_t] _get_dst_hours(
519519
trans_idx = mismatch.nonzero()[0]
520520

521521
if trans_idx.size == 1:
522-
# TODO: not reached in tests 2022-05-02; possible?
522+
# see test_tz_localize_to_utc_ambiguous_infer
523523
stamp = _render_tstamp(vals[trans_idx[0]], reso=reso)
524524
raise pytz.AmbiguousTimeError(
525525
f"Cannot infer dst time from {stamp} as there "
@@ -541,15 +541,15 @@ cdef ndarray[int64_t] _get_dst_hours(
541541

542542
delta = np.diff(result_a[grp])
543543
if grp.size == 1 or np.all(delta > 0):
544-
# TODO: not reached in tests 2022-05-02; possible?
544+
# see test_tz_localize_to_utc_ambiguous_infer
545545
stamp = _render_tstamp(vals[grp[0]], reso=reso)
546546
raise pytz.AmbiguousTimeError(stamp)
547547

548548
# Find the index for the switch and pull from a for dst and b
549549
# for standard
550550
switch_idxs = (delta <= 0).nonzero()[0]
551551
if switch_idxs.size > 1:
552-
# TODO: not reached in tests 2022-05-02; possible?
552+
# see test_tz_localize_to_utc_ambiguous_infer
553553
raise pytz.AmbiguousTimeError(
554554
f"There are {switch_idxs.size} dst switches when "
555555
"there should only be 1."

pandas/core/arrays/datetimelike.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,12 +1088,13 @@ def _cmp_method(self, other, op):
10881088

10891089
@final
10901090
def _add_datetimelike_scalar(self, other):
1091-
# Overridden by TimedeltaArray
10921091
if not is_timedelta64_dtype(self.dtype):
10931092
raise TypeError(
10941093
f"cannot add {type(self).__name__} and {type(other).__name__}"
10951094
)
10961095

1096+
self = cast("TimedeltaArray", self)
1097+
10971098
from pandas.core.arrays import DatetimeArray
10981099

10991100
assert other is not NaT
@@ -1115,7 +1116,7 @@ def _add_datetimelike_scalar(self, other):
11151116
return DatetimeArray(result, dtype=dtype, freq=self.freq)
11161117

11171118
@final
1118-
def _add_datetime_arraylike(self, other):
1119+
def _add_datetime_arraylike(self, other) -> DatetimeArray:
11191120
if not is_timedelta64_dtype(self.dtype):
11201121
raise TypeError(
11211122
f"cannot add {type(self).__name__} and {type(other).__name__}"
@@ -1679,12 +1680,11 @@ def median(self, *, axis: int | None = None, skipna: bool = True, **kwargs):
16791680
return self._wrap_reduction_result(axis, result)
16801681

16811682
def _mode(self, dropna: bool = True):
1682-
values = self
1683+
mask = None
16831684
if dropna:
1684-
mask = values.isna()
1685-
values = values[~mask]
1685+
mask = self.isna()
16861686

1687-
i8modes = mode(values.view("i8"))
1687+
i8modes = mode(self.view("i8"), mask=mask)
16881688
npmodes = i8modes.view(self._ndarray.dtype)
16891689
npmodes = cast(np.ndarray, npmodes)
16901690
return self._from_backing_data(npmodes)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import numpy as np
2+
import pytest
3+
import pytz
4+
5+
from pandas._libs.tslibs.tzconversion import tz_localize_to_utc
6+
7+
8+
class TestTZLocalizeToUTC:
9+
def test_tz_localize_to_utc_ambiguous_infer(self):
10+
# val is a timestamp that is ambiguous when localized to US/Eastern
11+
val = 1_320_541_200_000_000_000
12+
vals = np.array([val, val - 1, val], dtype=np.int64)
13+
14+
with pytest.raises(pytz.AmbiguousTimeError, match="2011-11-06 01:00:00"):
15+
tz_localize_to_utc(vals, pytz.timezone("US/Eastern"), ambiguous="infer")
16+
17+
with pytest.raises(pytz.AmbiguousTimeError, match="are no repeated times"):
18+
tz_localize_to_utc(vals[:1], pytz.timezone("US/Eastern"), ambiguous="infer")
19+
20+
vals[1] += 1
21+
msg = "There are 2 dst switches when there should only be 1"
22+
with pytest.raises(pytz.AmbiguousTimeError, match=msg):
23+
tz_localize_to_utc(vals, pytz.timezone("US/Eastern"), ambiguous="infer")

0 commit comments

Comments
 (0)