PYield 0.1.4__tar.gz → 0.1.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: PYield
3
- Version: 0.1.4
3
+ Version: 0.1.6
4
4
  Summary: A Python library for analysis of fixed income instruments in Brazil
5
5
  Keywords: fixed-income, brazil, finance, analysis, bonds
6
6
  Author-Email: Carlos Carvalho <cr.cj@outlook.com>
@@ -1,4 +1,4 @@
1
1
  # SPDX-FileCopyrightText: 2023-present crcj <cr.cj@outlook.com>
2
2
  #
3
3
  # SPDX-License-Identifier: MIT
4
- __version__ = "0.1.4"
4
+ __version__ = "0.1.6"
@@ -118,6 +118,8 @@ def get_raw_di_data(reference_date: pd.Timestamp) -> pd.DataFrame:
118
118
  header=1,
119
119
  thousands=".",
120
120
  decimal=",",
121
+ # Avoid parsing "-" as a string. See column "AJUSTE CORRIG. (4)" in 02/01/2006.
122
+ na_values=["-"],
121
123
  dtype_backend="numpy_nullable",
122
124
  )[0]
123
125
  # Remove rows with all NaN values
@@ -126,10 +128,9 @@ def get_raw_di_data(reference_date: pd.Timestamp) -> pd.DataFrame:
126
128
  df = df.dropna(axis=1, how="all")
127
129
  return df
128
130
 
129
- except ValueError:
130
- # Emit a warning indicating the absence of data for the specific date
131
+ except Exception as e:
131
132
  warnings.warn(
132
- f"No data found for the date {reference_date.strftime('%d/%m/%Y')}. Returning an empty DataFrame."
133
+ f"A {type(e).__name__} occurred while reading the DI futures data for {reference_date.strftime("%d/%m/%Y")}. Is this a valid date? Returning an empty DataFrame."
133
134
  )
134
135
  return pd.DataFrame()
135
136
 
@@ -157,14 +158,14 @@ def process_di_data(df: pd.DataFrame, reference_date: pd.Timestamp) -> pd.DataFr
157
158
  "NÚM. NEGOC.": "number_of_trades",
158
159
  "CONTR. NEGOC.": "trading_volume",
159
160
  "VOL.": "financial_volume",
160
- "AJUSTE ANTER. (3)": "prev_settlement",
161
- "AJUSTE CORRIG. (4)": "adj_prev_settlement",
161
+ "AJUSTE ANTER. (3)": "prev_settlement_rate",
162
+ "AJUSTE CORRIG. (4)": "adj_prev_settlement_rate",
162
163
  "PREÇO ABERTU.": "opening_rate",
163
164
  "PREÇO MÍN.": "min_rate",
164
165
  "PREÇO MÁX.": "max_rate",
165
166
  "PREÇO MÉD.": "avg_rate",
166
- "ÚLT. PREÇO": "last_rate",
167
- "AJUSTE": "settlement",
167
+ "ÚLT. PREÇO": "closing_rate",
168
+ "AJUSTE": "settlement_rate",
168
169
  "VAR. PTOS.": "point_variation",
169
170
  "ÚLT.OF. COMPRA": "last_bid",
170
171
  "ÚLT.OF. VENDA": "last_offer",
@@ -180,30 +181,53 @@ def process_di_data(df: pd.DataFrame, reference_date: pd.Timestamp) -> pd.DataFr
180
181
  df["maturity"] = df["contract_code"].apply(convert_contract_code)
181
182
 
182
183
  df["bday"] = wd.count_business_days(reference_date, df["maturity"])
184
+ # Remove rows with bday <= 0
185
+ df = df[df["bday"] > 0]
186
+
187
+ # Columns where 0 means NaN
188
+ cols_with_nan = [
189
+ "prev_settlement_rate",
190
+ "adj_prev_settlement_rate",
191
+ "settlement_rate",
192
+ "opening_rate",
193
+ "min_rate",
194
+ "max_rate",
195
+ "avg_rate",
196
+ "closing_rate",
197
+ "last_bid",
198
+ "last_offer",
199
+ ]
200
+ for col in cols_with_nan:
201
+ df[cols_with_nan] = df[cols_with_nan].replace(0, np.nan)
202
+
203
+ # Convert prices to rates in settlement columns
204
+ settlement_cols = [
205
+ "prev_settlement_rate",
206
+ "adj_prev_settlement_rate",
207
+ "settlement_rate",
208
+ ]
209
+ for col in settlement_cols:
210
+ df[col] = (100_000 / df[col]) ** (252 / df["bday"]) - 1
211
+ # Convert to percentage and round to 3 decimal places
212
+ df[col] = (100 * df[col]).round(3)
183
213
 
184
- df["settlement_rate"] = (100_000 / df["settlement"]) ** (252 / df["bday"]) - 1
185
- # Convert to percentage and round to 3 decimal places
186
- df["settlement_rate"] = (100 * df["settlement_rate"]).round(3)
187
-
188
- # Prior to 22/05/2006, prices were not converted to rates
189
- rate_columns = [
214
+ # Prior to 01/01/2002, prices were not converted to rates
215
+ convert_cols = [
190
216
  "opening_rate",
191
217
  "min_rate",
192
218
  "max_rate",
193
219
  "avg_rate",
194
- "last_rate",
220
+ "closing_rate",
195
221
  "last_bid",
196
222
  "last_offer",
197
223
  ]
198
- if reference_date < pd.Timestamp("2006-05-22"):
199
- for col in rate_columns:
200
- # Force conversion to float, since sometimes column is read as int
201
- df[col] = df[col].astype(float)
202
- df[col] = np.where(
203
- df[col] == 0, 0, (100_000 / df[col]) ** (252 / df["bday"]) - 1
204
- )
224
+ if reference_date < pd.Timestamp("2002-01-01"):
225
+ for col in convert_cols:
226
+ df[col] = (100_000 / df[col]) ** (252 / df["bday"]) - 1
205
227
  # Convert to percentage and round to 3 decimal places
206
228
  df[col] = (100 * df[col]).round(3)
229
+ # Invert low and high prices
230
+ df["min_rate"], df["max_rate"] = df["max_rate"], df["min_rate"]
207
231
 
208
232
  # Order columns
209
233
  df = df[
@@ -215,16 +239,15 @@ def process_di_data(df: pd.DataFrame, reference_date: pd.Timestamp) -> pd.DataFr
215
239
  "number_of_trades",
216
240
  "trading_volume",
217
241
  "financial_volume",
218
- "prev_settlement",
219
- "adj_prev_settlement",
220
- "settlement",
221
242
  "opening_rate",
222
243
  "min_rate",
223
244
  "max_rate",
224
245
  "avg_rate",
225
- "last_rate",
246
+ "closing_rate",
226
247
  "last_bid",
227
248
  "last_offer",
249
+ "prev_settlement_rate",
250
+ "adj_prev_settlement_rate",
228
251
  "settlement_rate",
229
252
  ]
230
253
  ]
@@ -20,6 +20,17 @@ HOLIDAYS_NPA = df["date"].values.astype("datetime64[D]")
20
20
  # HOLIDAYS_STR = df["date"].dt.strftime("%Y-%m-%d").to_list()
21
21
 
22
22
 
23
+ def convert_to_numpy_date(dates):
24
+ if isinstance(dates, pd.Timestamp):
25
+ return dates.to_numpy().astype("datetime64[D]")
26
+ else:
27
+ return dates.values.astype("datetime64[D]")
28
+
29
+
30
+ def adjust_to_next_business_day(dates):
31
+ return np.busday_offset(dates, 0, roll="forward", holidays=HOLIDAYS_NPA)
32
+
33
+
23
34
  def count_business_days(
24
35
  start_dates: pd.Series | pd.Timestamp,
25
36
  end_dates: pd.Series | pd.Timestamp,
@@ -55,16 +66,12 @@ def count_business_days(
55
66
  >>> bday_count(start_series, end_series)
56
67
  array([22, 18])
57
68
  """
58
- # Convert start_dates to the corresponding numpy type
59
- if isinstance(start_dates, pd.Timestamp):
60
- start_dates = start_dates.to_numpy().astype("datetime64[D]")
61
- else:
62
- start_dates = start_dates.values.astype("datetime64[D]")
69
+ # Convert to numpy date format
70
+ start_dates = convert_to_numpy_date(start_dates)
71
+ end_dates = convert_to_numpy_date(end_dates)
63
72
 
64
- # Convert end_dates to the corresponding numpy type
65
- if isinstance(end_dates, pd.Timestamp):
66
- end_dates = end_dates.to_numpy().astype("datetime64[D]")
67
- else:
68
- end_dates = end_dates.values.astype("datetime64[D]")
73
+ # Adjust start and end dates to the next business day if they fall on a holiday or weekend
74
+ start_dates = adjust_to_next_business_day(start_dates)
75
+ end_dates = adjust_to_next_business_day(end_dates)
69
76
 
70
77
  return np.busday_count(start_dates, end_dates, holidays=HOLIDAYS_NPA)
@@ -31,7 +31,7 @@ dependencies = [
31
31
  "lxml",
32
32
  ]
33
33
  dynamic = []
34
- version = "0.1.4"
34
+ version = "0.1.6"
35
35
 
36
36
  [project.license]
37
37
  file = "LICENSE"
File without changes
File without changes
File without changes
File without changes
File without changes