numpy / numpy/numpy-financial

Rework functions to use ufuncs?

Open
#23 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement help wanted question
Dominant language
Python
Stars
409
Forks
98
Avg merge
10h 43m
Merged PRs (30d)
12

Description

The implementation of e.g. nper:

https://github.com/numpy/numpy-financial/blob/master/numpy_financial/_financial.py#L229

requires the usual “split the array up using boolean indexes and then do different things to each hunk” stuff that you have to do when writing a function with conditional logic that operates on an entire array. This tends to lead to convoluted, hard-to-maintain code. (And nper still has more edge cases that need to be handled, so there will need to be more boolean arrays!)

One solution to this problem is to write ufuncs so that you can work with scalars and just use conditional logic. The numpy_financial functions can’t be full ufuncs because they have default arguments, but most of them can be very thin wrappers around a ufunc.

The disadvantage of introducing ufuncs is that you introduce compiled code, but perhaps it is worth it? Code for generating the loops from scalar kernels can be grabbed from SciPy (though it can be stripped down for numpy_financial since less languages need to be supported).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in numpy_financial/_financial.py around nper and compare its boolean-indexing implementation with the scalar-kernel and loop-generation approach referenced from SciPy. Review the other financial functions and the edge cases mentioned in the issue before assessing scope. Done requires an agreed approach, affected functions, and edge-case coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
data
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.