PYield 0.35.8__tar.gz → 0.35.9__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.
Files changed (52) hide show
  1. {pyield-0.35.8 → pyield-0.35.9}/PKG-INFO +1 -1
  2. pyield-0.35.9/pyield/__about__.py +1 -0
  3. {pyield-0.35.8 → pyield-0.35.9}/pyield/bday.py +128 -95
  4. pyield-0.35.8/pyield/__about__.py +0 -1
  5. {pyield-0.35.8 → pyield-0.35.9}/.gitignore +0 -0
  6. {pyield-0.35.8 → pyield-0.35.9}/LICENSE +0 -0
  7. {pyield-0.35.8 → pyield-0.35.9}/README.md +0 -0
  8. {pyield-0.35.8 → pyield-0.35.9}/pyield/__init__.py +0 -0
  9. {pyield-0.35.8 → pyield-0.35.9}/pyield/anbima/__init__.py +0 -0
  10. {pyield-0.35.8 → pyield-0.35.9}/pyield/anbima/ettj.py +0 -0
  11. {pyield-0.35.8 → pyield-0.35.9}/pyield/anbima/ima.py +0 -0
  12. {pyield-0.35.8 → pyield-0.35.9}/pyield/anbima/imaq.py +0 -0
  13. {pyield-0.35.8 → pyield-0.35.9}/pyield/anbima/ipca.py +0 -0
  14. {pyield-0.35.8 → pyield-0.35.9}/pyield/anbima/tpf.py +0 -0
  15. {pyield-0.35.8 → pyield-0.35.9}/pyield/b3/__init__.py +0 -0
  16. {pyield-0.35.8 → pyield-0.35.9}/pyield/b3/di1.py +0 -0
  17. {pyield-0.35.8 → pyield-0.35.9}/pyield/b3/di_over.py +0 -0
  18. {pyield-0.35.8 → pyield-0.35.9}/pyield/b3/futures/__init__.py +0 -0
  19. {pyield-0.35.8 → pyield-0.35.9}/pyield/b3/futures/common.py +0 -0
  20. {pyield-0.35.8 → pyield-0.35.9}/pyield/b3/futures/historical.py +0 -0
  21. {pyield-0.35.8 → pyield-0.35.9}/pyield/b3/futures/intraday.py +0 -0
  22. {pyield-0.35.8 → pyield-0.35.9}/pyield/b3/futures/xml.py +0 -0
  23. {pyield-0.35.8 → pyield-0.35.9}/pyield/bc/__init__.py +0 -0
  24. {pyield-0.35.8 → pyield-0.35.9}/pyield/bc/auction.py +0 -0
  25. {pyield-0.35.8 → pyield-0.35.9}/pyield/bc/bcdata.py +0 -0
  26. {pyield-0.35.8 → pyield-0.35.9}/pyield/bc/ptax.py +0 -0
  27. {pyield-0.35.8 → pyield-0.35.9}/pyield/bc/repo.py +0 -0
  28. {pyield-0.35.8 → pyield-0.35.9}/pyield/bc/trades_intraday.py +0 -0
  29. {pyield-0.35.8 → pyield-0.35.9}/pyield/bc/trades_monthly.py +0 -0
  30. {pyield-0.35.8 → pyield-0.35.9}/pyield/bc/vna.py +0 -0
  31. {pyield-0.35.8 → pyield-0.35.9}/pyield/conftest.py +0 -0
  32. {pyield-0.35.8 → pyield-0.35.9}/pyield/data_cache.py +0 -0
  33. {pyield-0.35.8 → pyield-0.35.9}/pyield/date_converter.py +0 -0
  34. {pyield-0.35.8 → pyield-0.35.9}/pyield/fwd.py +0 -0
  35. {pyield-0.35.8 → pyield-0.35.9}/pyield/holidays/__init__.py +0 -0
  36. {pyield-0.35.8 → pyield-0.35.9}/pyield/holidays/br_holidays_new.txt +0 -0
  37. {pyield-0.35.8 → pyield-0.35.9}/pyield/holidays/br_holidays_old.txt +0 -0
  38. {pyield-0.35.8 → pyield-0.35.9}/pyield/holidays/brholidays.py +0 -0
  39. {pyield-0.35.8 → pyield-0.35.9}/pyield/ibge/__init__.py +0 -0
  40. {pyield-0.35.8 → pyield-0.35.9}/pyield/ibge/ipca.py +0 -0
  41. {pyield-0.35.8 → pyield-0.35.9}/pyield/interpolator.py +0 -0
  42. {pyield-0.35.8 → pyield-0.35.9}/pyield/py.typed +0 -0
  43. {pyield-0.35.8 → pyield-0.35.9}/pyield/retry.py +0 -0
  44. {pyield-0.35.8 → pyield-0.35.9}/pyield/tn/__init__.py +0 -0
  45. {pyield-0.35.8 → pyield-0.35.9}/pyield/tn/auction.py +0 -0
  46. {pyield-0.35.8 → pyield-0.35.9}/pyield/tn/lft.py +0 -0
  47. {pyield-0.35.8 → pyield-0.35.9}/pyield/tn/ltn.py +0 -0
  48. {pyield-0.35.8 → pyield-0.35.9}/pyield/tn/ntnb.py +0 -0
  49. {pyield-0.35.8 → pyield-0.35.9}/pyield/tn/ntnc.py +0 -0
  50. {pyield-0.35.8 → pyield-0.35.9}/pyield/tn/ntnf.py +0 -0
  51. {pyield-0.35.8 → pyield-0.35.9}/pyield/tn/tools.py +0 -0
  52. {pyield-0.35.8 → pyield-0.35.9}/pyproject.toml +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PYield
3
- Version: 0.35.8
3
+ Version: 0.35.9
4
4
  Summary: A Python library for analysis of fixed income instruments in Brazil
5
5
  Project-URL: Source, https://github.com/crdcj/PYield
6
6
  Author-email: Carlos Carvalho <cr.cj@outlook.com>
@@ -0,0 +1 @@
1
+ __version__ = "0.35.9"
@@ -44,7 +44,7 @@ def offset(
44
44
  ) -> pd.Series: ...
45
45
 
46
46
 
47
- def offset(
47
+ def offset( # noqa
48
48
  dates: DateScalar | DateArray,
49
49
  offset: IntegerScalar | IntegerArray,
50
50
  roll: Literal["forward", "backward"] = "forward",
@@ -56,8 +56,11 @@ def offset(
56
56
  dates. It is a wrapper for `numpy.busday_offset` adapted for Pandas data types and
57
57
  brazilian holidays.
58
58
 
59
- **Important Note:** The `dates` parameter is used to determine which list of
60
- holidays applies to the calculation.
59
+ **Important Note:** Each date in the `dates` input is evaluated individually to
60
+ determine which list of holidays applies to the calculation. Transition date
61
+ is 2023-12-26, which means:
62
+ - Dates before 2023-12-26 use the old holiday list.
63
+ - Dates on or after 2023-12-26 use the new holiday list.
61
64
 
62
65
  Args:
63
66
  dates (DateScalar | DateArray): The date(s) to offset. Can be a scalar date type
@@ -150,6 +153,18 @@ def offset(
150
153
  1 NaT
151
154
  dtype: datetime64[ns]
152
155
 
156
+ Original pandas index is preserved
157
+
158
+ >>> dates = pd.Series(
159
+ ... ["19-09-2024", "20-09-2024", "21-09-2024"],
160
+ ... index=["a", "b", "c"],
161
+ ... )
162
+ >>> bday.offset(dates, 1)
163
+ a 2024-09-20
164
+ b 2024-09-23
165
+ c 2024-09-24
166
+ dtype: datetime64[ns]
167
+
153
168
  Note:
154
169
  This function uses `numpy.busday_offset` under the hood, which means it follows
155
170
  the same conventions and limitations for business day calculations. For detailed
@@ -157,70 +172,89 @@ def offset(
157
172
  documentation:
158
173
  https://numpy.org/doc/stable/reference/generated/numpy.busday_offset.html
159
174
  """
175
+ # Padroniza as entradas para objetos do Pandas
160
176
  dates_pd = dc.convert_input_dates(dates)
177
+ offset_pd = pd.Series(offset) if isinstance(offset, IntegerArray) else offset
178
+
179
+ # 1. VALIDAÇÃO ESTRUTURAL
180
+ if isinstance(dates_pd, pd.Series) and isinstance(offset_pd, pd.Series):
181
+ if not dates_pd.index.equals(offset_pd.index):
182
+ raise ValueError("Input dates have different indices!")
183
+
184
+ is_scalar_input = not (
185
+ isinstance(dates_pd, pd.Series) or isinstance(offset_pd, pd.Series)
186
+ )
161
187
 
162
- # Antecipate NaT handling
163
- if dates_pd is pd.NaT:
164
- if isinstance(offset, (list, tuple, np.ndarray, pd.Series)):
165
- # If offset is a list, return a Series of NaT with the same length
166
- return pd.Series([pd.NaT] * len(offset), dtype="datetime64[ns]")
167
- else:
188
+ # 2. LÓGICA PRINCIPAL
189
+
190
+ # --- CASO 1: AMBOS OS INPUTS SÃO ESCALARES
191
+ if is_scalar_input:
192
+ if pd.isna(dates_pd) or pd.isna(offset_pd):
168
193
  return pd.NaT
169
194
 
170
- if isinstance(dates_pd, pd.Series):
171
- # Divide the input in order to apply the correct holiday list
172
- old_dates = dates_pd[dates_pd < br_holidays.TRANSITION_DATE]
173
- new_dates = dates_pd[dates_pd >= br_holidays.TRANSITION_DATE]
174
- # nat = NaT = Not a Time = missing value
175
- nat_dates = dates_pd[dates_pd.isna()]
176
-
177
- offsetted_old_dates = np.busday_offset(
178
- dc.to_numpy_date_type(old_dates),
179
- offsets=offset,
195
+ offsetted_date_np = np.busday_offset(
196
+ dc.to_numpy_date_type(dates_pd),
197
+ offsets=offset_pd,
180
198
  roll=roll,
181
- holidays=OLD_HOLIDAYS_ARRAY,
199
+ holidays=br_holidays.get_holiday_array(dates_pd),
182
200
  )
183
201
 
184
- offsetted_new_dates = np.busday_offset(
185
- dc.to_numpy_date_type(new_dates),
186
- offsets=offset,
187
- roll=roll,
188
- holidays=NEW_HOLIDAYS_ARRAY,
189
- )
202
+ return pd.Timestamp(offsetted_date_np).as_unit("ns")
190
203
 
191
- # Convert from numpy.datetime64 to pandas.Timestamp
192
- offsetted_old_dates = pd.to_datetime(offsetted_old_dates)
193
- offsetted_new_dates = pd.to_datetime(offsetted_new_dates)
204
+ # --- CASO 2: PELO MENOS UM DOS INPUTS É UMA SÉRIE ---
205
+ if isinstance(dates_pd, pd.Series) or isinstance(offset_pd, pd.Series):
206
+ # Garante que ambas as variáveis sejam Series para alinhamento automático.
207
+ # O Pandas faz o "broadcast" do escalar para o tamanho da Series.
208
+ if not isinstance(dates_pd, pd.Series):
209
+ # dates escalar e offset Series -> Series de dates com o índice de offset.
210
+ dates_pd = pd.Series(dates_pd, index=offset_pd.index)
211
+ if not isinstance(offset_pd, pd.Series):
212
+ # offset escalar e dates Series -> Series de offset com o índice de dates.
213
+ offset_pd = pd.Series(offset_pd, index=dates_pd.index)
194
214
 
195
- # 'pd.to_datetime' does not necessarily return datetime64[ns] Series
196
- offsetted_old_dates = pd.Series(offsetted_old_dates).astype("datetime64[ns]")
197
- offsetted_new_dates = pd.Series(offsetted_new_dates).astype("datetime64[ns]")
215
+ # Inicializa a série de resultados com o índice correto e tipo que suporta NaT.
216
+ result = pd.Series(index=dates_pd.index, dtype="datetime64[ns]")
198
217
 
199
- # Use old index to rejoin the results
200
- offsetted_old_dates.index = old_dates.index
201
- offsetted_new_dates.index = new_dates.index
218
+ # Divide the input in order to apply the correct holiday list
219
+ mask_not_null = dates_pd.notna() & offset_pd.notna()
220
+ mask_old_holidays = mask_not_null & (dates_pd < br_holidays.TRANSITION_DATE)
221
+ mask_new_holidays = mask_not_null & (dates_pd >= br_holidays.TRANSITION_DATE)
202
222
 
203
- offsetted_dates = pd.concat(
204
- [offsetted_old_dates, offsetted_new_dates, nat_dates]
205
- )
223
+ if mask_old_holidays.any():
224
+ np_offset_values = np.busday_offset(
225
+ dates=dc.to_numpy_date_type(dates_pd[mask_old_holidays]),
226
+ offsets=offset_pd[mask_old_holidays],
227
+ roll=roll,
228
+ holidays=OLD_HOLIDAYS_ARRAY,
229
+ )
230
+ pd_offset_values = pd.to_datetime(np_offset_values).astype("datetime64[ns]")
231
+ result.loc[mask_old_holidays] = pd_offset_values
206
232
 
207
- # Reorder the result to match the original input order
208
- return offsetted_dates.sort_index()
233
+ if mask_new_holidays.any():
234
+ np_offset_values = np.busday_offset(
235
+ dates=dc.to_numpy_date_type(dates_pd[mask_new_holidays]),
236
+ offsets=offset_pd[mask_new_holidays],
237
+ roll=roll,
238
+ holidays=NEW_HOLIDAYS_ARRAY,
239
+ )
240
+ pd_offset_values = pd.to_datetime(np_offset_values).astype("datetime64[ns]")
241
+ result.loc[mask_new_holidays] = pd_offset_values
209
242
 
210
- elif isinstance(dates_pd, pd.Timestamp):
211
- offsetted_dates_np = np.busday_offset(
212
- dc.to_numpy_date_type(dates_pd),
213
- offsets=offset,
214
- roll=roll,
215
- holidays=br_holidays.get_holiday_array(dates_pd),
216
- )
217
- if isinstance(offsetted_dates_np, np.datetime64):
218
- return pd.Timestamp(offsetted_dates_np).as_unit("ns")
219
- else:
220
- return pd.Series(offsetted_dates_np).astype("datetime64[ns]")
243
+ return result
221
244
 
222
- else:
223
- raise ValueError("Invalid input type for 'dates'.")
245
+ # --- CASO 3: A ENTRADA 'dates' É UM ESCALAR E 'offset' É UMA SÉRIE ---
246
+ result = pd.Series(index=offset_pd.index, dtype="datetime64[ns]")
247
+ offset_dates_np = np.busday_offset(
248
+ dates=dc.to_numpy_date_type(dates_pd),
249
+ offsets=offset_pd,
250
+ roll=roll,
251
+ holidays=br_holidays.get_holiday_array(dates_pd),
252
+ )
253
+ # Convert from numpy.datetime64 to datetime64[ns] (pandas format)
254
+ offset_dates_pd = pd.to_datetime(offset_dates_np).astype("datetime64[ns]")
255
+ return pd.Series(
256
+ offset_dates_pd.values, index=offset_pd.index, dtype="datetime64[ns]"
257
+ )
224
258
 
225
259
 
226
260
  @overload
@@ -250,8 +284,11 @@ def count( # noqa
250
284
  on the inputs. It accounts for specified holidays, effectively excluding them from
251
285
  the business day count.
252
286
 
253
- **Important Note:** The `start` date is used to determine which list of holidays
254
- applies to the calculation.
287
+ **Important Note:** Each date in the `start` input is evaluated individually to
288
+ determine which list of holidays (old or new) applies to the calculation. The
289
+ transition date is 2023-12-26, which means:
290
+ - Dates before 2023-12-26 use the old holiday list.
291
+ - Dates on or after 2023-12-26 use the new holiday list.
255
292
 
256
293
  Args:
257
294
  start (DateScalar | DateArray): The start date(s) for counting (inclusive).
@@ -333,8 +370,6 @@ def count( # noqa
333
370
 
334
371
  # 1. VALIDAÇÃO ESTRUTURAL
335
372
  if isinstance(start_pd, pd.Series) and isinstance(end_pd, pd.Series):
336
- if len(start_pd) != len(end_pd):
337
- raise ValueError("Input Series must have the same length.")
338
373
  if not start_pd.index.equals(end_pd.index):
339
374
  raise ValueError("Input dates have different indices!")
340
375
 
@@ -348,53 +383,51 @@ def count( # noqa
348
383
  if pd.isna(start_pd) or pd.isna(end_pd):
349
384
  return pd.NA
350
385
 
351
- holidays = br_holidays.get_holiday_array(start_pd)
352
386
  result = np.busday_count(
353
387
  begindates=dc.to_numpy_date_type(start_pd),
354
388
  enddates=dc.to_numpy_date_type(end_pd),
355
- holidays=holidays,
389
+ holidays=br_holidays.get_holiday_array(start_pd),
356
390
  )
357
391
  return int(result)
358
392
 
359
393
  # --- CASO 2: PELO MENOS UMA ENTRADA É UMA SÉRIE ---
360
- else:
361
- # Garante que ambas as variáveis sejam Series para alinhamento automático.
362
- # O Pandas faz o "broadcast" do escalar para o tamanho da Series.
363
- if not isinstance(start_pd, pd.Series):
364
- # start escalar e end Series: cria uma Series de start com o índice de end.
365
- start_pd = pd.Series(start_pd, index=end_pd.index)
366
- if not isinstance(end_pd, pd.Series):
367
- # end escalar e start Series: cria uma Series de end com o índice de start.
368
- end_pd = pd.Series(end_pd, index=start_pd.index)
369
-
370
- # Inicializa a série de resultados com o índice correto e tipo que suporta NA.
371
- # Nulos nas entradas (start_pd ou end_pd) resultarão em NA aqui.
372
- result = pd.Series(index=start_pd.index, dtype="Int64")
373
-
374
- # Cria máscaras para linhas não-nulas, divididas pelo calendário de feriados.
375
- # A lógica de feriados depende APENAS da data de início.
376
- not_null_mask = start_pd.notna() & end_pd.notna()
377
- mask_old_holidays = not_null_mask & (start_pd < br_holidays.TRANSITION_DATE)
378
- mask_new_holidays = not_null_mask & (start_pd >= br_holidays.TRANSITION_DATE)
379
-
380
- # Calcula e atribui os resultados para o subconjunto "OLD"
381
- if mask_old_holidays.any():
382
- result.loc[mask_old_holidays] = np.busday_count(
383
- begindates=dc.to_numpy_date_type(start_pd[mask_old_holidays]),
384
- enddates=dc.to_numpy_date_type(end_pd[mask_old_holidays]),
385
- holidays=OLD_HOLIDAYS_ARRAY,
386
- )
394
+ # Garante que ambas as variáveis sejam Series para alinhamento automático.
395
+ # O Pandas faz o "broadcast" do escalar para o tamanho da Series.
396
+ if not isinstance(start_pd, pd.Series):
397
+ # start escalar e end Series: cria uma Series de start com o índice de end.
398
+ start_pd = pd.Series(start_pd, index=end_pd.index)
399
+ if not isinstance(end_pd, pd.Series):
400
+ # end escalar e start Series: cria uma Series de end com o índice de start.
401
+ end_pd = pd.Series(end_pd, index=start_pd.index)
402
+
403
+ # Inicializa a série de resultados com o índice correto e tipo que suporta NA.
404
+ # Nulos nas entradas (start_pd ou end_pd) resultarão em NA aqui.
405
+ result = pd.Series(index=start_pd.index, dtype="Int64")
406
+
407
+ # Cria máscaras para linhas não-nulas, divididas pelo calendário de feriados.
408
+ # A lógica de feriados depende APENAS da data de início.
409
+ not_null_mask = start_pd.notna() & end_pd.notna()
410
+ mask_old_holidays = not_null_mask & (start_pd < br_holidays.TRANSITION_DATE)
411
+ mask_new_holidays = not_null_mask & (start_pd >= br_holidays.TRANSITION_DATE)
412
+
413
+ # Calcula e atribui os resultados para o subconjunto "OLD"
414
+ if mask_old_holidays.any():
415
+ result.loc[mask_old_holidays] = np.busday_count(
416
+ begindates=dc.to_numpy_date_type(start_pd[mask_old_holidays]),
417
+ enddates=dc.to_numpy_date_type(end_pd[mask_old_holidays]),
418
+ holidays=OLD_HOLIDAYS_ARRAY,
419
+ )
387
420
 
388
- # Calcula e atribui os resultados para o subconjunto "NEW"
389
- if mask_new_holidays.any():
390
- result.loc[mask_new_holidays] = np.busday_count(
391
- begindates=dc.to_numpy_date_type(start_pd[mask_new_holidays]),
392
- enddates=dc.to_numpy_date_type(end_pd[mask_new_holidays]),
393
- holidays=NEW_HOLIDAYS_ARRAY,
394
- )
421
+ # Calcula e atribui os resultados para o subconjunto "NEW"
422
+ if mask_new_holidays.any():
423
+ result.loc[mask_new_holidays] = np.busday_count(
424
+ begindates=dc.to_numpy_date_type(start_pd[mask_new_holidays]),
425
+ enddates=dc.to_numpy_date_type(end_pd[mask_new_holidays]),
426
+ holidays=NEW_HOLIDAYS_ARRAY,
427
+ )
395
428
 
396
- # As linhas onde not_null_mask era False já contêm o valor <NA> default.
397
- return result
429
+ # As linhas onde not_null_mask era False já contêm o valor <NA> default.
430
+ return result
398
431
 
399
432
 
400
433
  def generate(
@@ -1 +0,0 @@
1
- __version__ = "0.35.8"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes