Skip to content

Enable dict for Triangle.fillna() value parameter. #1026

@genedan

Description

@genedan

Description

Pandas DataFrame.fillna() can take a dict for the value parameter. This enables you to have a different fill value per column:

df = pd.DataFrame(
    [
        [np.nan, 2, np.nan, 0],
        [3, 4, np.nan, 1],
        [np.nan, np.nan, np.nan, np.nan],
        [np.nan, 3, np.nan, 4],
    ],
    columns=list("ABCD"),
)
df
     A    B   C    D
0  NaN  2.0 NaN  0.0
1  3.0  4.0 NaN  1.0
2  NaN  NaN NaN  NaN
3  NaN  3.0 NaN  4.0
values = {"A": 0, "B": 1, "C": 2, "D": 3}
df.fillna(value=values)
     A    B    C    D
0  0.0  2.0  2.0  0.0
1  3.0  4.0  2.0  1.0
2  0.0  1.0  2.0  3.0
3  0.0  3.0  2.0  4.0

Is your feature request aligned with the scope of the package?

  • Yes, absolutely!
  • No, but it's still worth discussing.
  • N/A (this request is not a codebase enhancement).

Describe the solution you'd like, or your current workaround.

We can enable an analogue for Triangle.fillna(). Here we have the desire to have a different fill value due to the different magnitudes in paid losses vs claim counts:

import chainladder as cl
import numpy as np

tri = cl.Triangle(
    data={
        'origin': [1985, 1985, 1985, 1986, 1986, 1987],
        'development': [1985, 1986, 1987, 1986, 1987, 1987],
        'paid': [np.nan, 600, 700, np.nan, 600, 500],
        'claim count': [np.nan, 2, 3, np.nan, 2, 1]
    },
    origin='origin',
    development='development',
    columns=['paid', 'claim count'],
    cumulative=True
)
print(tri['paid'])
print(tri['claim count'])

         12     24     36
1985    NaN  600.0  700.0
1986    NaN  600.0    NaN
1987  500.0    NaN    NaN
       12   24   36
1985  NaN  2.0  3.0
1986  NaN  2.0  NaN
1987  1.0  NaN  NaN
tri = tri.fillna({'paid': 500, 'claim count': 1})

print(tri['paid'])
print(tri['claim count'])

         12     24     36
1985    500 600.0  700.0
1986    500  600.0    NaN
1987  500.0    NaN    NaN
       12   24   36
1985  1.0  2.0  3.0
1986  1.0  2.0  NaN
1987  1.0  NaN  NaN

Do you have any additional supporting notes?

No response

Would you be willing to contribute this ticket?

  • Yes, absolutely!
  • Yes, but I would like some help.
  • No.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Priority

    Low

    Effort

    Low

    Scope

    Codebase

    Projects

    Status
    Backlog

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions