-
-
Notifications
You must be signed in to change notification settings - Fork 18.9k
Closed
Labels
EnhancementNeeds DiscussionRequires discussion from core team before further actionRequires discussion from core team before further actionPeriodPeriod data typePeriod data typeTimezonesTimezone data dtypeTimezone data dtype
Description
Is your feature request related to a problem?
A Timestamp
with a freq
attribute can be used as a replacement/workaround for a Period
, if time-zone awareness is needed. Below is an example.
With the deprecation of the .freq
attribute for individual timestamps, this workaround will stop working in a future release.
Describe the solution you'd like
Implementation of timezone-awareness in the Period
class.
API breaking implications
Period
and PeriodIndex
initializers take an additional tz
parameter.
Describe alternatives you've considered
The alternative is what I'm currently using (Timestamp.freq
) but will be deprecated.
Additional context
Current implementation of this functionality:
import pandas as pd
def ts_right(ts_left: pd.Timestamp) -> pd.Timestamp:
"""Right-bound timestamp associated with a (left-bound) timestamp."""
if ts_left.freq == "D":
kwargs = {"days": 1}
elif ts_left.freq == "MS":
kwargs = {"months": 1}
elif ts_left.freq == "QS":
kwargs = {"months": 3}
elif ts_left.freq == "AS":
kwargs = {"years": 1}
else:
raise ValueError(f"Invalid frequency: {ts_left.freq}.")
return ts_left + pd.DateOffset(**kwargs)
def duration(ts_left: pd.Timestamp) -> float:
"""Duration [h] associated with a timestamp."""
return (ts_right(ts_left) - ts_left).total_seconds() / 3600
ts1 = pd.Timestamp("2022-03-01", tz="Europe/Berlin", freq="D") # "Timestamp.freq is deprecated and will be removed in a future version"
ts_right(ts1) # Timestamp('2022-03-02 00:00:00+0100', tz='Europe/Berlin')
duration(ts1) # 24.0
ts2 = pd.Timestamp("2022-03-27", tz="Europe/Berlin", freq="D") # "Timestamp.freq is deprecated and will be removed in a future version"
ts_right(ts2) # Timestamp('2022-03-28 00:00:00+0200', tz='Europe/Berlin')
duration(ts2) # 23.0
swertz and EmanueleMason
Metadata
Metadata
Assignees
Labels
EnhancementNeeds DiscussionRequires discussion from core team before further actionRequires discussion from core team before further actionPeriodPeriod data typePeriod data typeTimezonesTimezone data dtypeTimezone data dtype