Skip to content

Commit

Permalink
update ver
Browse files Browse the repository at this point in the history
  • Loading branch information
mazarimono committed Nov 8, 2023
1 parent 02ce24a commit f99483d
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 26 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
## 開発メモ


### add-new-feature
### ver0.2

- お値段の通貨選択機能の追加を検討中。
- add to_forex method to j_realty_en
-


### 20231020
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ pref_code = j.pref_code
city_code = j.city_code('kyoto')
pt = jrn.PropTransactions(pref_code, city_code, 20223, 20224)
df = pt.get_data()

# Can get Prices with forex(USD, EUR, CNY) (from ver0.2)
df_forex = pt.to_forex(df, 'USD')
```

- 日本語
Expand Down
43 changes: 41 additions & 2 deletions j_realty_api/j_realty_en.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pandas as pd
import pkg_resources
import requests
from JPYForex import jpyforex

# API from 国土交通省
# web: https://www.land.mlit.go.jp/webland/api.html
Expand Down Expand Up @@ -115,8 +116,8 @@ def get_data(self) -> pd.DataFrame:
try:
df = pd.DataFrame(r.json()["data"])
df["Period"] = df["Period"].map(self.custom_to_datetime)
# for col in ['TradePrice']: # 'Area'は文字列のものが入っているので、対応を考えて追加する
# df[col] = df[col].astype(int)
df['TradePrice'] = df['TradePrice'].astype("Int64")
df['UnitPrice'] = pd.to_numeric(df['UnitPrice'], errors='ignore')
return df
except KeyError:
print(
Expand All @@ -128,6 +129,35 @@ def get_data(self) -> pd.DataFrame:
else:
raise Exception(f"Status_code: {r.status_code}")


def to_forex(self, df: pd.DataFrame, currency: str) -> pd.DataFrame:
'''
Params:
df: pd.DataFrame
DataFrame from get_data()
currency: str
select from 3types: "USD", "EUR", "CNY"
Returns:
pd.DataFrame
'''
dates = df['Period'].unique()
min_date = dates.min().to_timestamp().date()
max_date = dates.max().to_timestamp(how='End').date()
min_date_str = self.dt_to_string(min_date)
max_date_str = self.dt_to_string(max_date)
cur = jpyforex.JPYForex(currency=currency, freq='Q', start_date=min_date_str, end_date=max_date_str)
data = cur.get_data()
data.index = data.index.to_period('Q')
forex_data = dict()
for i in data.index:
forex_data[i] = data.loc[i].values[0]
df['forex'] = df['Period'].map(forex_data)
df['ForexTradePrice'] = df.apply(lambda x: x['TradePrice'] / x['forex'], axis=1)
df['ForexUnitPrice'] = df.apply(lambda x: x['UnitPrice'] / x['forex'], axis=1)
return df


def custom_to_datetime(self, p_str):
split_str = p_str.split(" ")
year = int(split_str[2])
Expand All @@ -147,12 +177,21 @@ def custom_to_datetime(self, p_str):
else:
raise ValueError(f"invalid quater: {p_str}")


def dt_to_qt(self, year, month):
dt = datetime(year, month, 1)
ts = pd.Timestamp(dt)
qt = ts.to_period("Q")
return qt


def dt_to_string(self, dt: datetime) -> str:
year = dt.year
month = dt.month
date = dt.day
str_dt = f'{year}{month:02}{date:02}'
return str_dt


if __name__ == "__main__":
k = CityCode("Kyoto")
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "j_realty_api"
version = "0.1"
version = "0.2"
description = "j_realty_api: Knowing the real estate transaction prices in Japan."
authors= [{name = "HideyukiO", email="hogawa098@gmail.com"}]
readme = "README.md"
Expand All @@ -16,7 +16,7 @@ classifiers = [
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
]
dependencies = ["requests", "pandas"]
dependencies = ["requests", "pandas", "jpyforex"]

[tool.setuptools]
include-package-data = true
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pandas
requests
jpyforex
Loading

0 comments on commit f99483d

Please sign in to comment.