Skip to content

BUG: Fix timezone-related indexing and plotting bugs #27367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,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. ``np.minimum``) to a timezone aware :class:`Series` (:issue:`15552`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just FYI, we have NumPy in our inter-sphinx config so :meth:`numpy.minimum` would work.


Numeric
^^^^^^^
Expand Down Expand Up @@ -1054,7 +1055,7 @@ Indexing
- Bug in :class:`CategoricalIndex` and :class:`Categorical` incorrectly raising ``ValueError`` instead of ``TypeError`` when a list is passed using the ``in`` operator (``__contains__``) (:issue:`21729`)
- 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 :class:`Series` setting a existing tuple key (``__setitem__``) with timezone-aware datetime values incorrectly raising ``TypeError`` (:issue:`20441`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a -> an


Missing
^^^^^^^
Expand Down Expand Up @@ -1111,7 +1112,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
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,10 @@ def setitem(key, value):
def _set_with_engine(self, key, value):
values = self._values
try:
if is_extension_array_dtype(values):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you move this outside of the try/except?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is values an array-like or list-like? Can we do is_extension_array_dtype(values.dtype)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. Changed to pass the dtype instead

# The cython indexing routines do not support ExtensionArrays.
# Defer to the next setting routine.
raise KeyError
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this go outside the try/except and instead of raising just copy line 1260? it's one-liner either way

self.index._engine.set_value(values, key, value)
return
except KeyError:
Expand Down
3 changes: 0 additions & 3 deletions pandas/plotting/_matplotlib/converter.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
11 changes: 11 additions & 0 deletions pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,3 +847,14 @@ def test_head_tail(test_data):
assert_series_equal(test_data.series.head(0), test_data.series[0:0])
assert_series_equal(test_data.series.tail(), test_data.series[-5:])
assert_series_equal(test_data.series.tail(0), test_data.series[0:0])


def test_setitem_tuple_with_datetimetz():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you move to test_datetime (same dir)

# GH 20441
arr = pd.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)
10 changes: 9 additions & 1 deletion pandas/tests/series/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from pandas._libs.tslibs import conversion, timezones

from pandas import DatetimeIndex, Index, NaT, Series, Timestamp
from pandas import DatetimeIndex, Index, NaT, Series, Timestamp, to_datetime
from pandas.core.indexes.datetimes import date_range
import pandas.util.testing as tm

Expand Down Expand Up @@ -379,3 +379,11 @@ def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture):
result = Series([Timestamp("2019", tz=tz)], dtype="datetime64[ns]")
expected = Series([Timestamp("2019")])
tm.assert_series_equal(result, expected)

def test_numpy_reduction_with_tz_aware_dtype(self, tz_aware_fixture):
# GH 15552
tz = tz_aware_fixture
arg = to_datetime(["2019"]).tz_localize(tz)
expected = Series(arg)
result = np.minimum(expected, expected)
tm.assert_series_equal(result, expected)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might belong in tests.reductions? Especially if it can be extended to the other box classes