Skip to content

Commit 0d2ec3e

Browse files
mroeschkejreback
authored andcommitted
BUG: Fix timezone-related indexing and plotting bugs (#27367)
1 parent f1684a1 commit 0d2ec3e

File tree

6 files changed

+31
-8
lines changed

6 files changed

+31
-8
lines changed

doc/source/whatsnew/v0.25.0.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,7 @@ Timezones
10361036
- Bug in :func:`DataFrame.join` where joining a timezone aware index with a timezone aware column would result in a column of ``NaN`` (:issue:`26335`)
10371037
- Bug in :func:`date_range` where ambiguous or nonexistent start or end times were not handled by the ``ambiguous`` or ``nonexistent`` keywords respectively (:issue:`27088`)
10381038
- Bug in :meth:`DatetimeIndex.union` when combining a timezone aware and timezone unaware :class:`DatetimeIndex` (:issue:`21671`)
1039+
- Bug when applying a numpy reduction function (e.g. :meth:`numpy.minimum`) to a timezone aware :class:`Series` (:issue:`15552`)
10391040
10401041
Numeric
10411042
^^^^^^^
@@ -1096,7 +1097,7 @@ Indexing
10961097
- Bug in setting a new value in a :class:`Series` with a :class:`Timedelta` object incorrectly casting the value to an integer (:issue:`22717`)
10971098
- Bug in :class:`Series` setting a new key (``__setitem__``) with a timezone-aware datetime incorrectly raising ``ValueError`` (:issue:`12862`)
10981099
- Bug in :meth:`DataFrame.iloc` when indexing with a read-only indexer (:issue:`17192`)
1099-
-
1100+
- Bug in :class:`Series` setting an existing tuple key (``__setitem__``) with timezone-aware datetime values incorrectly raising ``TypeError`` (:issue:`20441`)
11001101
11011102
Missing
11021103
^^^^^^^
@@ -1153,7 +1154,7 @@ Plotting
11531154
- Bug in an error message in :meth:`DataFrame.plot`. Improved the error message if non-numerics are passed to :meth:`DataFrame.plot` (:issue:`25481`)
11541155
- Bug in incorrect ticklabel positions when plotting an index that are non-numeric / non-datetime (:issue:`7612`, :issue:`15912`, :issue:`22334`)
11551156
- Fixed bug causing plots of :class:`PeriodIndex` timeseries to fail if the frequency is a multiple of the frequency rule code (:issue:`14763`)
1156-
-
1157+
- Fixed bug when plotting a :class:`DatetimeIndex` with ``datetime.timezone.utc`` timezone (:issue:`17173`)
11571158
-
11581159
-
11591160

pandas/core/series.py

+4
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,10 @@ def setitem(key, value):
12361236

12371237
def _set_with_engine(self, key, value):
12381238
values = self._values
1239+
if is_extension_array_dtype(values.dtype):
1240+
# The cython indexing engine does not support ExtensionArrays.
1241+
values[self.index.get_loc(key)] = value
1242+
return
12391243
try:
12401244
self.index._engine.set_value(values, key, value)
12411245
return

pandas/plotting/_matplotlib/converter.py

-3
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,6 @@ def axisinfo(unit, axis):
324324
class PandasAutoDateFormatter(dates.AutoDateFormatter):
325325
def __init__(self, locator, tz=None, defaultfmt="%Y-%m-%d"):
326326
dates.AutoDateFormatter.__init__(self, locator, tz, defaultfmt)
327-
# matplotlib.dates._UTC has no _utcoffset called by pandas
328-
if self._tz is dates.UTC:
329-
self._tz._utcoffset = self._tz.utcoffset(None)
330327

331328

332329
class PandasAutoDateLocator(dates.AutoDateLocator):

pandas/tests/plotting/test_datetimelike.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ def teardown_method(self, method):
4545
tm.close()
4646

4747
@pytest.mark.slow
48-
def test_ts_plot_with_tz(self):
49-
# GH2877
50-
index = date_range("1/1/2011", periods=2, freq="H", tz="Europe/Brussels")
48+
def test_ts_plot_with_tz(self, tz_aware_fixture):
49+
# GH2877, GH17173
50+
tz = tz_aware_fixture
51+
index = date_range("1/1/2011", periods=2, freq="H", tz=tz)
5152
ts = Series([188.5, 328.25], index=index)
5253
_check_plot_works(ts.plot)
5354

pandas/tests/reductions/test_reductions.py

+9
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,15 @@ def test_same_tz_min_max_axis_1(self, op, expected_col):
159159
expected = df[expected_col].rename(None)
160160
tm.assert_series_equal(result, expected)
161161

162+
@pytest.mark.parametrize("func", ["maximum", "minimum"])
163+
def test_numpy_reduction_with_tz_aware_dtype(self, tz_aware_fixture, func):
164+
# GH 15552
165+
tz = tz_aware_fixture
166+
arg = pd.to_datetime(["2019"]).tz_localize(tz)
167+
expected = Series(arg)
168+
result = getattr(np, func)(expected, expected)
169+
tm.assert_series_equal(result, expected)
170+
162171

163172
class TestIndexReductions:
164173
# Note: the name TestIndexReductions indicates these tests

pandas/tests/series/indexing/test_datetime.py

+11
Original file line numberDiff line numberDiff line change
@@ -765,3 +765,14 @@ def test_round_nat(method, freq):
765765
expected = Series(pd.NaT)
766766
round_method = getattr(s.dt, method)
767767
assert_series_equal(round_method(freq), expected)
768+
769+
770+
def test_setitem_tuple_with_datetimetz():
771+
# GH 20441
772+
arr = date_range("2017", periods=4, tz="US/Eastern")
773+
index = [(0, 1), (0, 2), (0, 3), (0, 4)]
774+
result = Series(arr, index=index)
775+
expected = result.copy()
776+
result[(0, 1)] = np.nan
777+
expected.iloc[0] = np.nan
778+
assert_series_equal(result, expected)

0 commit comments

Comments
 (0)