Skip to content

Commit b4f5d42

Browse files
committed
Rephrase whatsnew entry, move tests
1 parent 1ed1423 commit b4f5d42

File tree

3 files changed

+69
-34
lines changed

3 files changed

+69
-34
lines changed

doc/source/whatsnew/v1.0.0.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ Categorical
133133

134134
- Added test to assert the :func:`fillna` raises the correct ValueError message when the value isn't a value from categories (:issue:`13628`)
135135
- Bug in :meth:`Categorical.astype` where ``NaN`` values were handled incorrectly when casting to int (:issue:`28406`)
136-
-
136+
- Using date accessors on a categorical dtyped series of datetimes was not returning a Series (or DataFrame) of the
137+
same type as if one used the .str. / .dt. on a Series of that type, e.g. when accessing :meth:`Series.dt.tz_localize` on a
138+
:class:`Categorical` with duplicate entries, the accessor was skipping duplicates (:issue: `27952`)
137139
-
138140

139141

pandas/tests/indexes/test_category.py

-33
Original file line numberDiff line numberDiff line change
@@ -1123,36 +1123,3 @@ def test_engine_type(self, dtype, engine_type):
11231123
ci.values._codes = ci.values._codes.astype("int64")
11241124
assert np.issubdtype(ci.codes.dtype, dtype)
11251125
assert isinstance(ci._engine, engine_type)
1126-
1127-
def test_dt_tz_localize(self, tz_aware_fixture):
1128-
# GH 27952
1129-
tz = tz_aware_fixture
1130-
datetimes = pd.Series(
1131-
["2019-01-01", "2019-01-01", "2019-01-02"], dtype="datetime64[ns]"
1132-
)
1133-
categorical = datetimes.astype("category")
1134-
result = categorical.dt.tz_localize(tz)
1135-
expected = datetimes.dt.tz_localize(tz)
1136-
tm.assert_series_equal(result, expected)
1137-
1138-
def test_dt_tz_convert(self, tz_aware_fixture):
1139-
# GH 27952
1140-
tz = tz_aware_fixture
1141-
datetimes = pd.Series(
1142-
["2019-01-01", "2019-01-01", "2019-01-02"], dtype="datetime64[ns, MET]"
1143-
)
1144-
categorical = datetimes.astype("category")
1145-
result = categorical.dt.tz_convert(tz)
1146-
expected = datetimes.dt.tz_convert(tz)
1147-
tm.assert_series_equal(result, expected)
1148-
1149-
@pytest.mark.parametrize("accessor", ["year", "month", "day"])
1150-
def test_dt_other_accessors(self, accessor):
1151-
# GH 27952
1152-
datetimes = pd.Series(
1153-
["2018-01-01", "2018-01-01", "2019-01-02"], dtype="datetime64[ns]"
1154-
)
1155-
categorical = datetimes.astype("category")
1156-
result = getattr(categorical.dt, accessor)
1157-
expected = getattr(datetimes.dt, accessor)
1158-
tm.assert_series_equal(result, expected)

pandas/tests/series/test_datetime_values.py

+66
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,72 @@ def test_dt_namespace_accessor_categorical(self):
345345
expected = Series([2017, 2017, 2018, 2018], name="foo")
346346
tm.assert_series_equal(result, expected)
347347

348+
def test_dt_tz_localize(self, tz_aware_fixture):
349+
# GH 27952
350+
tz = tz_aware_fixture
351+
datetimes = pd.Series(
352+
["2019-01-01", "2019-01-01", "2019-01-02"], dtype="datetime64[ns]"
353+
)
354+
categorical = datetimes.astype("category")
355+
result = categorical.dt.tz_localize(tz)
356+
expected = datetimes.dt.tz_localize(tz)
357+
tm.assert_series_equal(result, expected)
358+
359+
def test_dt_tz_convert(self, tz_aware_fixture):
360+
# GH 27952
361+
tz = tz_aware_fixture
362+
datetimes = pd.Series(
363+
["2019-01-01", "2019-01-01", "2019-01-02"], dtype="datetime64[ns, MET]"
364+
)
365+
categorical = datetimes.astype("category")
366+
result = categorical.dt.tz_convert(tz)
367+
expected = datetimes.dt.tz_convert(tz)
368+
tm.assert_series_equal(result, expected)
369+
370+
@pytest.mark.parametrize("accessor", ["year", "month", "day"])
371+
def test_dt_other_accessors(self, accessor):
372+
# GH 27952
373+
datetimes = pd.Series(
374+
["2018-01-01", "2018-01-01", "2019-01-02"], dtype="datetime64[ns]"
375+
)
376+
categorical = datetimes.astype("category")
377+
result = getattr(categorical.dt, accessor)
378+
expected = getattr(datetimes.dt, accessor)
379+
tm.assert_series_equal(result, expected)
380+
381+
def test_dt_tz_localize(self, tz_aware_fixture):
382+
# GH 27952
383+
tz = tz_aware_fixture
384+
datetimes = pd.Series(
385+
["2019-01-01", "2019-01-01", "2019-01-02"], dtype="datetime64[ns]"
386+
)
387+
categorical = datetimes.astype("category")
388+
result = categorical.dt.tz_localize(tz)
389+
expected = datetimes.dt.tz_localize(tz)
390+
tm.assert_series_equal(result, expected)
391+
392+
def test_dt_tz_convert(self, tz_aware_fixture):
393+
# GH 27952
394+
tz = tz_aware_fixture
395+
datetimes = pd.Series(
396+
["2019-01-01", "2019-01-01", "2019-01-02"], dtype="datetime64[ns, MET]"
397+
)
398+
categorical = datetimes.astype("category")
399+
result = categorical.dt.tz_convert(tz)
400+
expected = datetimes.dt.tz_convert(tz)
401+
tm.assert_series_equal(result, expected)
402+
403+
@pytest.mark.parametrize("accessor", ["year", "month", "day"])
404+
def test_dt_other_accessors(self, accessor):
405+
# GH 27952
406+
datetimes = pd.Series(
407+
["2018-01-01", "2018-01-01", "2019-01-02"], dtype="datetime64[ns]"
408+
)
409+
categorical = datetimes.astype("category")
410+
result = getattr(categorical.dt, accessor)
411+
expected = getattr(datetimes.dt, accessor)
412+
tm.assert_series_equal(result, expected)
413+
348414
def test_dt_accessor_no_new_attributes(self):
349415
# https://github.com/pandas-dev/pandas/issues/10673
350416
s = Series(date_range("20130101", periods=5, freq="D"))

0 commit comments

Comments
 (0)