Skip to content

Commit

Permalink
BUG: Fix timezone-related indexing and plotting bugs (#27367)
Browse files Browse the repository at this point in the history
  • Loading branch information
mroeschke authored and jreback committed Jul 17, 2019
1 parent f1684a1 commit 0d2ec3e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
5 changes: 3 additions & 2 deletions doc/source/whatsnew/v0.25.0.rst
Expand Up @@ -1036,6 +1036,7 @@ Timezones
- 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`)
- 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`)
- Bug in :meth:`DatetimeIndex.union` when combining a timezone aware and timezone unaware :class:`DatetimeIndex` (:issue:`21671`)
- Bug when applying a numpy reduction function (e.g. :meth:`numpy.minimum`) to a timezone aware :class:`Series` (:issue:`15552`)
Numeric
^^^^^^^
Expand Down Expand Up @@ -1096,7 +1097,7 @@ Indexing
- Bug in setting a new value in a :class:`Series` with a :class:`Timedelta` object incorrectly casting the value to an integer (:issue:`22717`)
- Bug in :class:`Series` setting a new key (``__setitem__``) with a timezone-aware datetime incorrectly raising ``ValueError`` (:issue:`12862`)
- Bug in :meth:`DataFrame.iloc` when indexing with a read-only indexer (:issue:`17192`)
-
- Bug in :class:`Series` setting an existing tuple key (``__setitem__``) with timezone-aware datetime values incorrectly raising ``TypeError`` (:issue:`20441`)
Missing
^^^^^^^
Expand Down Expand Up @@ -1153,7 +1154,7 @@ Plotting
- Bug in an error message in :meth:`DataFrame.plot`. Improved the error message if non-numerics are passed to :meth:`DataFrame.plot` (:issue:`25481`)
- Bug in incorrect ticklabel positions when plotting an index that are non-numeric / non-datetime (:issue:`7612`, :issue:`15912`, :issue:`22334`)
- Fixed bug causing plots of :class:`PeriodIndex` timeseries to fail if the frequency is a multiple of the frequency rule code (:issue:`14763`)
-
- Fixed bug when plotting a :class:`DatetimeIndex` with ``datetime.timezone.utc`` timezone (:issue:`17173`)
-
-
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/series.py
Expand Up @@ -1236,6 +1236,10 @@ def setitem(key, value):

def _set_with_engine(self, key, value):
values = self._values
if is_extension_array_dtype(values.dtype):
# The cython indexing engine does not support ExtensionArrays.
values[self.index.get_loc(key)] = value
return
try:
self.index._engine.set_value(values, key, value)
return
Expand Down
3 changes: 0 additions & 3 deletions pandas/plotting/_matplotlib/converter.py
Expand Up @@ -324,9 +324,6 @@ def axisinfo(unit, axis):
class PandasAutoDateFormatter(dates.AutoDateFormatter):
def __init__(self, locator, tz=None, defaultfmt="%Y-%m-%d"):
dates.AutoDateFormatter.__init__(self, locator, tz, defaultfmt)
# matplotlib.dates._UTC has no _utcoffset called by pandas
if self._tz is dates.UTC:
self._tz._utcoffset = self._tz.utcoffset(None)


class PandasAutoDateLocator(dates.AutoDateLocator):
Expand Down
7 changes: 4 additions & 3 deletions pandas/tests/plotting/test_datetimelike.py
Expand Up @@ -45,9 +45,10 @@ def teardown_method(self, method):
tm.close()

@pytest.mark.slow
def test_ts_plot_with_tz(self):
# GH2877
index = date_range("1/1/2011", periods=2, freq="H", tz="Europe/Brussels")
def test_ts_plot_with_tz(self, tz_aware_fixture):
# GH2877, GH17173
tz = tz_aware_fixture
index = date_range("1/1/2011", periods=2, freq="H", tz=tz)
ts = Series([188.5, 328.25], index=index)
_check_plot_works(ts.plot)

Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/reductions/test_reductions.py
Expand Up @@ -159,6 +159,15 @@ def test_same_tz_min_max_axis_1(self, op, expected_col):
expected = df[expected_col].rename(None)
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize("func", ["maximum", "minimum"])
def test_numpy_reduction_with_tz_aware_dtype(self, tz_aware_fixture, func):
# GH 15552
tz = tz_aware_fixture
arg = pd.to_datetime(["2019"]).tz_localize(tz)
expected = Series(arg)
result = getattr(np, func)(expected, expected)
tm.assert_series_equal(result, expected)


class TestIndexReductions:
# Note: the name TestIndexReductions indicates these tests
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/series/indexing/test_datetime.py
Expand Up @@ -765,3 +765,14 @@ def test_round_nat(method, freq):
expected = Series(pd.NaT)
round_method = getattr(s.dt, method)
assert_series_equal(round_method(freq), expected)


def test_setitem_tuple_with_datetimetz():
# GH 20441
arr = date_range("2017", periods=4, tz="US/Eastern")
index = [(0, 1), (0, 2), (0, 3), (0, 4)]
result = Series(arr, index=index)
expected = result.copy()
result[(0, 1)] = np.nan
expected.iloc[0] = np.nan
assert_series_equal(result, expected)

0 comments on commit 0d2ec3e

Please sign in to comment.