()
| 989 | |
| 990 | |
| 991 | def test_date2num_dst(): |
| 992 | # Test for github issue #3896, but in date2num around DST transitions |
| 993 | # with a timezone-aware pandas date_range object. |
| 994 | |
| 995 | class dt_tzaware(datetime.datetime): |
| 996 | """ |
| 997 | This bug specifically occurs because of the normalization behavior of |
| 998 | pandas Timestamp objects, so in order to replicate it, we need a |
| 999 | datetime-like object that applies timezone normalization after |
| 1000 | subtraction. |
| 1001 | """ |
| 1002 | |
| 1003 | def __sub__(self, other): |
| 1004 | r = super().__sub__(other) |
| 1005 | tzinfo = getattr(r, 'tzinfo', None) |
| 1006 | |
| 1007 | if tzinfo is not None: |
| 1008 | localizer = getattr(tzinfo, 'normalize', None) |
| 1009 | if localizer is not None: |
| 1010 | r = tzinfo.normalize(r) |
| 1011 | |
| 1012 | if isinstance(r, datetime.datetime): |
| 1013 | r = self.mk_tzaware(r) |
| 1014 | |
| 1015 | return r |
| 1016 | |
| 1017 | def __add__(self, other): |
| 1018 | return self.mk_tzaware(super().__add__(other)) |
| 1019 | |
| 1020 | def astimezone(self, tzinfo): |
| 1021 | dt = super().astimezone(tzinfo) |
| 1022 | return self.mk_tzaware(dt) |
| 1023 | |
| 1024 | @classmethod |
| 1025 | def mk_tzaware(cls, datetime_obj): |
| 1026 | kwargs = {} |
| 1027 | attrs = ('year', |
| 1028 | 'month', |
| 1029 | 'day', |
| 1030 | 'hour', |
| 1031 | 'minute', |
| 1032 | 'second', |
| 1033 | 'microsecond', |
| 1034 | 'tzinfo') |
| 1035 | |
| 1036 | for attr in attrs: |
| 1037 | val = getattr(datetime_obj, attr, None) |
| 1038 | if val is not None: |
| 1039 | kwargs[attr] = val |
| 1040 | |
| 1041 | return cls(**kwargs) |
| 1042 | |
| 1043 | # Define a date_range function similar to pandas.date_range |
| 1044 | def date_range(start, freq, periods): |
| 1045 | dtstart = dt_tzaware.mk_tzaware(start) |
| 1046 | |
| 1047 | return [dtstart + (i * freq) for i in range(periods)] |
| 1048 |
nothing calls this directly
no test coverage detected
searching dependent graphs…