Skip to content

Commit 3deef38

Browse files
author
cloudboat
committed
fix the unreasonable change of none time zone
1 parent 726aa34 commit 3deef38

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

pandas/core/resample.py

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2573,49 +2573,45 @@ def _get_time_bins(self, ax: DatetimeIndex):
25732573
empty = DatetimeIndex(data=[], freq=self.freq, name=ax.name, dtype=ax.dtype)
25742574
return empty, [], empty
25752575

2576-
try:
2576+
def _calculate_bins_in_timezone(ax_to_use, tz):
2577+
"""Calculate time bins in specified timezone"""
25772578
first, last = _get_timestamp_range_edges(
2578-
ax.min(),
2579-
ax.max(),
2579+
ax_to_use.min(),
2580+
ax_to_use.max(),
25802581
self.freq,
25812582
unit=ax.unit,
25822583
closed=self.closed,
25832584
origin=self.origin,
25842585
offset=self.offset,
25852586
)
2586-
binner = labels = date_range(
2587+
return date_range(
25872588
freq=self.freq,
25882589
start=first,
25892590
end=last,
2590-
tz=ax.tz,
2591+
tz=tz,
25912592
name=ax.name,
25922593
ambiguous=True,
25932594
nonexistent="shift_forward",
25942595
unit=ax.unit,
25952596
)
2596-
except Exception:
2597-
# Fallback to UTC calculation for timezone-aware data
2598-
# to handle DST transition
2599-
# 62601
2600-
ax_utc = ax.tz_convert("UTC")
2601-
first_utc, last_utc = _get_timestamp_range_edges(
2602-
ax_utc.min(),
2603-
ax_utc.max(),
2604-
self.freq,
2605-
unit=ax.unit,
2606-
closed=self.closed,
2607-
origin=self.origin,
2608-
offset=self.offset,
2609-
)
2610-
binner_utc = date_range(
2611-
freq=self.freq,
2612-
start=first_utc,
2613-
end=last_utc,
2614-
tz="UTC",
2615-
name=ax.name,
2616-
unit=ax.unit,
2617-
)
2618-
binner = labels = binner_utc.tz_convert(ax.tz)
2597+
2598+
if ax.tz is not None:
2599+
try:
2600+
# normal way
2601+
binner = labels = _calculate_bins_in_timezone(ax, ax.tz)
2602+
except Exception as e:
2603+
if "nonexistent" in str(e).lower() or "ambiguous" in str(e).lower():
2604+
# Fallback to UTC calculation for timezone-aware data
2605+
# to handle DST transitions
2606+
# 62601
2607+
ax_utc = ax.tz_convert("UTC")
2608+
binner_utc = _calculate_bins_in_timezone(ax_utc, "UTC")
2609+
binner = labels = binner_utc.tz_convert(ax.tz)
2610+
else:
2611+
raise
2612+
else:
2613+
# no time zone
2614+
binner = labels = _calculate_bins_in_timezone(ax, None)
26192615

26202616
ax_values = ax.asi8
26212617
binner, bin_edges = self._adjust_bin_edges(binner, ax_values)

0 commit comments

Comments
 (0)