API/BUG: Enforce "normalized" pytz timezones for DatetimeIndex (#20510) · pandas-dev/pandas@fa24af9 (original) (raw)

`@@ -511,13 +511,7 @@ def _generate(cls, start, end, periods, name, offset,

`

511

511

`'different timezones')

`

512

512

``

513

513

`inferred_tz = timezones.maybe_get_tz(inferred_tz)

`

514

``

-

515

``

`-

these may need to be localized

`

516

514

`tz = timezones.maybe_get_tz(tz)

`

517

``

`-

if tz is not None:

`

518

``

`-

date = start or end

`

519

``

`-

if date.tzinfo is not None and hasattr(tz, 'localize'):

`

520

``

`-

tz = tz.localize(date.replace(tzinfo=None)).tzinfo

`

521

515

``

522

516

`if tz is not None and inferred_tz is not None:

`

523

517

`if not timezones.tz_compare(inferred_tz, tz):

`

`@@ -654,7 +648,8 @@ def _simple_new(cls, values, name=None, freq=None, tz=None,

`

654

648

`result._data = values

`

655

649

`result.name = name

`

656

650

`result.offset = freq

`

657

``

`-

result.tz = timezones.maybe_get_tz(tz)

`

``

651

`+

result._tz = timezones.maybe_get_tz(tz)

`

``

652

`+

result._tz = timezones.tz_standardize(result._tz)

`

658

653

`result._reset_identity()

`

659

654

`return result

`

660

655

``

`@@ -684,6 +679,17 @@ def _values(self):

`

684

679

`else:

`

685

680

`return self.values

`

686

681

``

``

682

`+

@property

`

``

683

`+

def tz(self):

`

``

684

`+

GH 18595

`

``

685

`+

return self._tz

`

``

686

+

``

687

`+

@tz.setter

`

``

688

`+

def tz(self, value):

`

``

689

`+

GH 3746: Prevent localizing or converting the index by setting tz

`

``

690

`+

raise AttributeError("Cannot directly set timezone. Use tz_localize() "

`

``

691

`+

"or tz_convert() as appropriate")

`

``

692

+

687

693

`@property

`

688

694

`def tzinfo(self):

`

689

695

`"""

`

`@@ -754,7 +760,7 @@ def _cached_range(cls, start=None, end=None, periods=None, offset=None,

`

754

760

``

755

761

`cachedRange = DatetimeIndex._simple_new(arr)

`

756

762

`cachedRange.offset = offset

`

757

``

`-

cachedRange.tz = None

`

``

763

`+

cachedRange = cachedRange.tz_localize(None)

`

758

764

`cachedRange.name = None

`

759

765

`drc[offset] = cachedRange

`

760

766

`else:

`

`@@ -831,7 +837,7 @@ def setstate(self, state):

`

831

837

``

832

838

`self.name = own_state[0]

`

833

839

`self.offset = own_state[1]

`

834

``

`-

self.tz = own_state[2]

`

``

840

`+

self._tz = timezones.tz_standardize(own_state[2])

`

835

841

``

836

842

`# provide numpy < 1.7 compat

`

837

843

`if nd_state[2] == 'M8[us]':

`

`@@ -1175,7 +1181,7 @@ def union(self, other):

`

1175

1181

`else:

`

1176

1182

`result = Index.union(this, other)

`

1177

1183

`if isinstance(result, DatetimeIndex):

`

1178

``

`-

result.tz = this.tz

`

``

1184

`+

result._tz = timezones.tz_standardize(this.tz)

`

1179

1185

`if (result.freq is None and

`

1180

1186

` (this.freq is not None or other.freq is not None)):

`

1181

1187

`result.offset = to_offset(result.inferred_freq)

`

`@@ -1223,7 +1229,7 @@ def union_many(self, others):

`

1223

1229

`tz = this.tz

`

1224

1230

`this = Index.union(this, other)

`

1225

1231

`if isinstance(this, DatetimeIndex):

`

1226

``

`-

this.tz = tz

`

``

1232

`+

this._tz = timezones.tz_standardize(tz)

`

1227

1233

``

1228

1234

`if this.freq is None:

`

1229

1235

`this.offset = to_offset(this.inferred_freq)

`