-
Notifications
You must be signed in to change notification settings - Fork 907
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
refactor(JP): remove arrow usage #6572
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
#!/usr/bin/env python3 | ||
from datetime import datetime, timedelta | ||
from datetime import datetime, time, timedelta | ||
from logging import Logger, getLogger | ||
from zoneinfo import ZoneInfo | ||
|
||
# The arrow library is used to handle datetimes | ||
import arrow | ||
import pandas as pd | ||
from requests import Session | ||
|
||
|
@@ -36,6 +35,7 @@ | |
"JP-ON": "www.okiden.co.jp/denki/", | ||
} | ||
ZONES_ONLY_LIVE = ["JP-TK", "JP-CB", "JP-SK"] | ||
TIMEZONE = ZoneInfo("Asia/Tokyo") | ||
|
||
|
||
def get_wind_capacity(datetime: datetime, zone_key, logger: Logger): | ||
|
@@ -156,7 +156,12 @@ def fetch_consumption_df( | |
""" | ||
if target_datetime is not None and zone_key in ZONES_ONLY_LIVE: | ||
raise NotImplementedError("This parser can only fetch live data") | ||
datestamp = arrow.get(target_datetime).to("Asia/Tokyo").strftime("%Y%m%d") | ||
|
||
if target_datetime is None: | ||
datestamp = datetime.now(tz=TIMEZONE).strftime("%Y%m%d") | ||
else: | ||
datestamp = target_datetime.replace(tzinfo=TIMEZONE).strftime("%Y%m%d") | ||
|
||
consumption_url = { | ||
"JP-HKD": f"http://denkiyoho.hepco.co.jp/area/data/juyo_01_{datestamp}.csv", | ||
"JP-TH": f"https://setsuden.nw.tohoku-epco.co.jp/common/demand/juyo_02_{datestamp}.csv", | ||
|
@@ -210,9 +215,14 @@ def fetch_consumption_forecast( | |
# Currently past dates not implemented for areas with no date in their demand csv files | ||
if target_datetime and zone_key == "JP-HKD": | ||
raise NotImplementedError("Past dates not yet implemented for selected region") | ||
datestamp = arrow.get(target_datetime).to("Asia/Tokyo").strftime("%Y%m%d") | ||
|
||
if target_datetime is None: | ||
datestamp = datetime.now(tz=TIMEZONE).strftime("%Y%m%d") | ||
else: | ||
datestamp = target_datetime.replace(tzinfo=TIMEZONE).strftime("%Y%m%d") | ||
|
||
# Forecasts ahead of current date are not available | ||
if datestamp > arrow.get().to("Asia/Tokyo").strftime("%Y%m%d"): | ||
if datestamp > datetime.now(tz=TIMEZONE).strftime("%Y%m%d"): | ||
raise NotImplementedError( | ||
"Future dates(local time) not implemented for selected region" | ||
) | ||
|
@@ -306,9 +316,10 @@ def fetch_price( | |
df = df[(df["Date"] >= start.date()) & (df["Date"] <= target_datetime.date())] | ||
|
||
df["datetime"] = df.apply( | ||
lambda row: arrow.get(row["Date"]) | ||
.shift(minutes=30 * (row["Period"] - 1)) | ||
.replace(tzinfo="Asia/Tokyo"), | ||
lambda row: ( | ||
datetime.combine(row["Date"], time(0, 0)) | ||
- timedelta(minutes=30 * (row["Period"] - 1)) | ||
).replace(tzinfo=TIMEZONE), | ||
axis=1, | ||
) | ||
|
||
|
@@ -329,26 +340,19 @@ def fetch_price( | |
return data | ||
|
||
|
||
def parse_dt(row): | ||
def parse_dt(row) -> datetime: | ||
"""Parses timestamps from date and time.""" | ||
if "AM" in row["Time"] or "PM" in row["Time"]: | ||
timestamp = ( | ||
arrow.get( | ||
" ".join([row["Date"], row["Time"]]).replace("/", "-"), | ||
"YYYY-M-D H:mm A", | ||
) | ||
.replace(tzinfo="Asia/Tokyo") | ||
.datetime | ||
date = row["Date"] | ||
time = row["Time"] | ||
Comment on lines
+345
to
+346
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it not be easier to parse these directly using the date and time libraries and then just combine them instead of using string manipulation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks only There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
https://docs.python.org/3.10/library/time.html#time.strptime You could also use the date time library directly on each of them and just call |
||
datetime_str = f"{date} {time}".replace("/", "-") | ||
if "AM" in time or "PM" in time: | ||
return datetime.strptime(datetime_str, "%Y-%m-%d %I:%M %p").replace( | ||
tzinfo=ZoneInfo("Asia/Tokyo") | ||
) | ||
else: | ||
timestamp = ( | ||
arrow.get( | ||
" ".join([row["Date"], row["Time"]]).replace("/", "-"), "YYYY-M-D H:mm" | ||
) | ||
.replace(tzinfo="Asia/Tokyo") | ||
.datetime | ||
return datetime.strptime(datetime_str, "%Y-%m-%d %H:%M").replace( | ||
tzinfo=ZoneInfo("Asia/Tokyo") | ||
) | ||
return timestamp | ||
|
||
|
||
if __name__ == "__main__": | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
target_datetime == None
,arrow.get(target_datetime)
returns the current time.