Skip to content
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

Update datetime_helpers.py #127

Closed
wants to merge 1 commit into from
Closed
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
55 changes: 28 additions & 27 deletions src/pytimetk/utils/datetime_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,35 +144,36 @@ def parse_freq_str(freq_str):
return quantity, unit

def freq_to_dateoffset(freq_str):
# Adjusted regex to account for potential absence of numeric part
unit_mapping = {
'D' : 'days',
'H' : 'hours',
'T' : 'minutes',
'min': 'minutes',
'S' : 'seconds',
'L' : 'milliseconds',
'U' : 'microseconds',
'N' : 'nanoseconds',
'Y' : 'years',
'A' : 'years',
'AS' : 'years',
'YS' : 'years',
'W' : 'weeks',
'Q' : 'months',
'QS' : 'months',
'M' : 'months',
'MS' : 'months',
}

quantity, unit = parse_freq_str(freq_str)

# Assume quantity of 1 if it's not explicitly provided
quantity = int(quantity) if quantity else 1

if unit == 'D': # Days
return pd.DateOffset(days=quantity)
elif unit == 'H': # Hours
return pd.DateOffset(hours=quantity)
elif unit == 'T' or unit == 'min': # Minutes
return pd.DateOffset(minutes=quantity)
elif unit == 'S': # Seconds
return pd.DateOffset(seconds=quantity)
elif unit == 'L': # Milliseconds
return pd.DateOffset(milliseconds=quantity)
elif unit == 'U': # Microseconds
return pd.DateOffset(microseconds=quantity)
elif unit == 'N': # Nanoseconds
return pd.DateOffset(nanoseconds=quantity)
elif unit in ['Y', 'A', 'AS', 'YS']: # Years
return pd.DateOffset(years=quantity)
elif unit == 'W': # Weeks
return pd.DateOffset(weeks=quantity)
elif unit in ['Q', 'QS']: # Quarters (approximated as 3*months)
return pd.DateOffset(months=quantity*3)
elif unit in ['M', 'MS']: # Months (approximated as 30.44 days)
return pd.DateOffset(months=quantity)
# ... add other units if needed

if unit in unit_mapping:
if unit_mapping[unit] == 'years':
return pd.DateOffset(years=quantity)
elif unit_mapping[unit] == 'months':
return pd.DateOffset(months=quantity)
else:
return pd.DateOffset(**{unit_mapping[unit]: quantity})
else:
raise ValueError(f"Unsupported frequency unit: {unit}")

Expand Down
Loading