Skip to content

Resolving Index Resetting for tz_localize #43226

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 25 commits into from
Sep 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ Timedelta

Timezones
^^^^^^^^^
-
- Bug in :meth:`Series.dt.tz_convert` resetting index in a :class:`Series` with :class:`CategoricalIndex` (:issue:`43080`)
-

Numeric
Expand Down Expand Up @@ -315,6 +315,7 @@ Indexing
- Bug in :meth:`DataFrame.drop` where the error message did not show missing labels with commas when raising ``KeyError`` (:issue:`42881`)
-


Missing
^^^^^^^
- Bug in :meth:`DataFrame.fillna` with limit and no method ignores axis='columns' or ``axis = 1`` (:issue:`40989`)
Expand Down
1 change: 1 addition & 0 deletions pandas/core/indexes/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ def __new__(cls, data: Series):
name=orig.name,
copy=False,
dtype=orig._values.categories.dtype,
index=orig.index,
)

if is_datetime64_dtype(data.dtype):
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/series/accessors/test_dt_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,19 @@ def test_isocalendar(self, input_series, expected_output):
)
tm.assert_frame_equal(result, expected_frame)

def test_hour_index(self):
dt_series = Series(
date_range(start="2021-01-01", periods=5, freq="h"),
index=[2, 6, 7, 8, 11],
dtype="category",
)
result = dt_series.dt.hour
expected = Series(
[0, 1, 2, 3, 4],
index=[2, 6, 7, 8, 11],
)
tm.assert_series_equal(result, expected)


class TestSeriesPeriodValuesDtAccessor:
@pytest.mark.parametrize(
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/series/methods/test_tz_localize.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ def test_series_tz_localize_ambiguous_bool(self):
result = ser.dt.tz_localize("US/Central", ambiguous=[False])
tm.assert_series_equal(result, expected1)

def test_series_tz_localize_matching_index(self):
# Matching the index of the result with that of the original series
# GH 43080
dt_series = Series(
date_range(start="2021-01-01T02:00:00", periods=5, freq="1D"),
index=[2, 6, 7, 8, 11],
dtype="category",
)
result = dt_series.dt.tz_localize("Europe/Berlin")
expected = Series(
date_range(
start="2021-01-01T02:00:00", periods=5, freq="1D", tz="Europe/Berlin"
),
index=[2, 6, 7, 8, 11],
)
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize("tz", ["Europe/Warsaw", "dateutil/Europe/Warsaw"])
@pytest.mark.parametrize(
"method, exp",
Expand Down