Skip to content

Fixed DatetimeIndex.tz_convert() .freq attribute #54532

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
19 changes: 19 additions & 0 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,25 @@ class DateOffset(RelativeDeltaOffset, metaclass=OffsetMeta):
previous midnight.
**kwds
Temporal parameter that add to or replace the offset value.
weekday : int {0, 1, ..., 6}, default 0

A specific integer for the day of the week.
- 0 is Monday
- 1 is Tuesday
- 2 is Wednesday
- 3 is Thursday
- 4 is Friday
- 5 is Saturday
- 6 is Sunday.

Instead Weekday type from dateutil.relativedelta can be used.
- MO is Monday
- TU is Tuesday
- WE is Wednesday
- TH is Thursday
- FR is Friday
- SA is Saturday
- SU is Sunday.

Parameters that **add** to the offset (like Timedelta):

Expand Down
9 changes: 5 additions & 4 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,8 @@ def _local_timestamps(self) -> npt.NDArray[np.int64]:

def tz_convert(self, tz) -> Self:
"""
Convert tz-aware Datetime Array/Index from one time zone to another.
Convert tz-aware Datetime Array/Index from one time zone to another
and sets frequency to None.

Parameters
----------
Expand Down Expand Up @@ -866,7 +867,7 @@ def tz_convert(self, tz) -> Self:
DatetimeIndex(['2014-08-01 02:00:00-05:00',
'2014-08-01 03:00:00-05:00',
'2014-08-01 04:00:00-05:00'],
dtype='datetime64[ns, US/Central]', freq='H')
dtype='datetime64[ns, US/Central]')

With the ``tz=None``, we can remove the timezone (after converting
to UTC if necessary):
Expand All @@ -884,7 +885,7 @@ def tz_convert(self, tz) -> Self:
DatetimeIndex(['2014-08-01 07:00:00',
'2014-08-01 08:00:00',
'2014-08-01 09:00:00'],
dtype='datetime64[ns]', freq='H')
dtype='datetime64[ns]')
"""
tz = timezones.maybe_get_tz(tz)

Expand All @@ -896,7 +897,7 @@ def tz_convert(self, tz) -> Self:

# No conversion since timestamps are all UTC to begin with
dtype = tz_to_dtype(tz, unit=self.unit)
return self._simple_new(self._ndarray, dtype=dtype, freq=self.freq)
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is an appropriate fix

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why do you think so? and what would be an appropriate fix?

Copy link
Member

Choose a reason for hiding this comment

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

Based on the failing tests seems like this behavior was relied upon

Copy link
Contributor Author

@Detoro Detoro Aug 16, 2023

Choose a reason for hiding this comment

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

Resolving this issue is undoing work done a while ago. The frequency attribute was set as part of this issue #25241- why do you think it should not be reset?

Copy link
Member

Choose a reason for hiding this comment

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

Based on the failing unit test the freq should be preserved

return self._simple_new(self._ndarray, dtype=dtype)

@dtl.ravel_compat
def tz_localize(
Expand Down
8 changes: 7 additions & 1 deletion pandas/tests/indexes/datetimes/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ def test_tz_convert_nat(self):
expected = ["2010-12-01 11:00", "2010-12-02 11:00", pd.NaT]
tm.assert_index_equal(idx, DatetimeIndex(expected, tz="US/Eastern"))

def test_tz_convert_freq(self):
i = date_range("2020-03-27 06:00", freq="D", periods=5, tz="Europe/Berlin")

i2 = i.tz_convert("UTC")
assert i2.freq is None

@pytest.mark.parametrize("prefix", ["", "dateutil/"])
def test_dti_tz_convert_compat_timestamp(self, prefix):
strdates = ["1/1/2012", "3/1/2012", "4/1/2012"]
Expand Down Expand Up @@ -880,7 +886,7 @@ def test_dti_tz_conversion_freq(self, tz_naive_fixture):
t3 = DatetimeIndex(["2019-01-01 10:00"], freq="H")
assert t3.tz_localize(tz=tz_naive_fixture).freq == t3.freq
t4 = DatetimeIndex(["2019-01-02 12:00"], tz="UTC", freq="T")
assert t4.tz_convert(tz="UTC").freq == t4.freq
assert t4.tz_convert(tz="UTC").freq is None

def test_drop_dst_boundary(self):
# see gh-18031
Expand Down