Skip to content

[TST] Write unit test for Triangle.fillna() when value is array - or deprecate this capability? #1032

@genedan

Description

@genedan

Description

There's not a unit test for Triangle.fillna() when the value is an array. However, it can be done, and the docstring was written with the intent that it'd be possible:

Image

Roping in @kennethshsu since your name is on this one 😆

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.

For example:

raa = cl.load_sample('raa').copy()
raa.values[0, 0, 4, 1] = np.nan
raa

         12       24       36       48       60       72       84       96       108      120
1981  5012.0   8269.0  10907.0  11805.0  13539.0  16181.0  18009.0  18608.0  18662.0  18834.0
1982   106.0   4285.0   5396.0  10666.0  13782.0  15599.0  15496.0  16169.0  16704.0      NaN
1983  3410.0   8992.0  13873.0  16141.0  18735.0  22214.0  22863.0  23466.0      NaN      NaN
1984  5655.0  11555.0  15766.0  21266.0  23425.0  26083.0  27067.0      NaN      NaN      NaN
1985  1092.0      NaN  15836.0  22169.0  25955.0  26180.0      NaN      NaN      NaN      NaN
1986  1513.0   6445.0  11702.0  12935.0  15852.0      NaN      NaN      NaN      NaN      NaN
1987   557.0   4020.0  10946.0  12314.0      NaN      NaN      NaN      NaN      NaN      NaN
1988  1351.0   6947.0  13112.0      NaN      NaN      NaN      NaN      NaN      NaN      NaN
1989  3133.0   5395.0      NaN      NaN      NaN      NaN      NaN      NaN      NaN      NaN
1990  2063.0      NaN      NaN      NaN      NaN      NaN      NaN      NaN      NaN      NaN
fill_by_dev = np.arange(1, 11).reshape(1, 1, 1, 10) * 1000.0
fill_by_dev

array([[[[ 1000.,  2000.,  3000.,  4000.,  5000.,  6000.,  7000.,
           8000.,  9000., 10000.]]]])
raa.fillna(fill_by_dev)

         12       24       36       48       60       72       84       96       108      120
1981  5012.0   8269.0  10907.0  11805.0  13539.0  16181.0  18009.0  18608.0  18662.0  18834.0
1982   106.0   4285.0   5396.0  10666.0  13782.0  15599.0  15496.0  16169.0  16704.0      NaN
1983  3410.0   8992.0  13873.0  16141.0  18735.0  22214.0  22863.0  23466.0      NaN      NaN
1984  5655.0  11555.0  15766.0  21266.0  23425.0  26083.0  27067.0      NaN      NaN      NaN
1985  1092.0   2000.0  15836.0  22169.0  25955.0  26180.0      NaN      NaN      NaN      NaN
1986  1513.0   6445.0  11702.0  12935.0  15852.0      NaN      NaN      NaN      NaN      NaN
1987   557.0   4020.0  10946.0  12314.0      NaN      NaN      NaN      NaN      NaN      NaN
1988  1351.0   6947.0  13112.0      NaN      NaN      NaN      NaN      NaN      NaN      NaN
1989  3133.0   5395.0      NaN      NaN      NaN      NaN      NaN      NaN      NaN      NaN
1990  2063.0      NaN      NaN      NaN      NaN      NaN      NaN      NaN      NaN      NaN

Do you have any additional supporting notes?

However, I'm not sure if this is a realistic use case or if this should actually be allowed. This deviates from the Pandas analogue which does not allow a list or ndarray to be passed:

df = pd.DataFrame({'a': [np.nan, 2, 3], 'b': [np.nan, 4, 5]})

     a    b
0  NaN  NaN
1  2.0  4.0
2  3.0  5.0

df.fillna([2, 3])
...
TypeError: "value" parameter must be a scalar or dict, but you passed a "list"

df.fillna(np.array([2,3]))
...
ValueError: invalid fill value with a <class 'numpy.ndarray'>

The use cases where you can pass a Series or dict to DataFrame.fillna() typically require you to map the fill values to columns. I think we should deprecate allowing an array, and support dict (see #1026) and Triangle (analogous to DataFrame) as supported inputs. I'd have to think a bit more as to what the use case would be for Series.

P.S. - anyone want to contribute to Pandas and get some street cred? This message should be updated to include Series and DataFrame as allowable inputs to value:

TypeError: "value" parameter must be a scalar or dict, but you passed a "list"

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

    Type

    Priority

    Low

    Effort

    Low

    Scope

    Codebase

    Projects

    Status
    Backlog

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions