Skip to content

Commit 35d52ff

Browse files
authored
Resolving Index Resetting for tz_localize (#43226)
* Resolving Index Resetting for tz_localize * Update test_tz_localize.py Adding a test case * Solving PEP-8 Issues * Changing Test Case and updating whatsnew * Changing Test Case and updating whatsnew * updating whatsnew * updating whatsnew * updating whatsnew * Making the necessary changes * Update test_tz_localize.py * Update test_tz_localize.py * Changes made * Update test_tz_localize.py * Commiting Again * Change in whatsnew * Update v1.4.0.rst Committing again with no changes to solve timeout error * Pre-commiting * Pre-commiting * New Test Case * Test Case Modified
1 parent 6a683a2 commit 35d52ff

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

doc/source/whatsnew/v1.4.0.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ Timedelta
322322

323323
Timezones
324324
^^^^^^^^^
325-
-
325+
- Bug in :meth:`Series.dt.tz_convert` resetting index in a :class:`Series` with :class:`CategoricalIndex` (:issue:`43080`)
326326
-
327327

328328
Numeric
@@ -361,6 +361,7 @@ Indexing
361361
- Bug in :meth:`DataFrame.drop` where the error message did not show missing labels with commas when raising ``KeyError`` (:issue:`42881`)
362362
-
363363

364+
364365
Missing
365366
^^^^^^^
366367
- Bug in :meth:`DataFrame.fillna` with limit and no method ignores axis='columns' or ``axis = 1`` (:issue:`40989`)

pandas/core/indexes/accessors.py

+1
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ def __new__(cls, data: Series):
492492
name=orig.name,
493493
copy=False,
494494
dtype=orig._values.categories.dtype,
495+
index=orig.index,
495496
)
496497

497498
if is_datetime64_dtype(data.dtype):

pandas/tests/series/accessors/test_dt_accessor.py

+13
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,19 @@ def test_isocalendar(self, input_series, expected_output):
691691
)
692692
tm.assert_frame_equal(result, expected_frame)
693693

694+
def test_hour_index(self):
695+
dt_series = Series(
696+
date_range(start="2021-01-01", periods=5, freq="h"),
697+
index=[2, 6, 7, 8, 11],
698+
dtype="category",
699+
)
700+
result = dt_series.dt.hour
701+
expected = Series(
702+
[0, 1, 2, 3, 4],
703+
index=[2, 6, 7, 8, 11],
704+
)
705+
tm.assert_series_equal(result, expected)
706+
694707

695708
class TestSeriesPeriodValuesDtAccessor:
696709
@pytest.mark.parametrize(

pandas/tests/series/methods/test_tz_localize.py

+17
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ def test_series_tz_localize_ambiguous_bool(self):
4141
result = ser.dt.tz_localize("US/Central", ambiguous=[False])
4242
tm.assert_series_equal(result, expected1)
4343

44+
def test_series_tz_localize_matching_index(self):
45+
# Matching the index of the result with that of the original series
46+
# GH 43080
47+
dt_series = Series(
48+
date_range(start="2021-01-01T02:00:00", periods=5, freq="1D"),
49+
index=[2, 6, 7, 8, 11],
50+
dtype="category",
51+
)
52+
result = dt_series.dt.tz_localize("Europe/Berlin")
53+
expected = Series(
54+
date_range(
55+
start="2021-01-01T02:00:00", periods=5, freq="1D", tz="Europe/Berlin"
56+
),
57+
index=[2, 6, 7, 8, 11],
58+
)
59+
tm.assert_series_equal(result, expected)
60+
4461
@pytest.mark.parametrize("tz", ["Europe/Warsaw", "dateutil/Europe/Warsaw"])
4562
@pytest.mark.parametrize(
4663
"method, exp",

0 commit comments

Comments
 (0)