Skip to content

fix(product_association): target_item rejects integer product ids #594

Open

Description

@murray-ds

What: ProductAssociation(target_item=...) rejects integer product ids, the most common kind. 10, np.int64(10) and [10] all raise TypeError: target_item must contain only str or float values. Checked on main @ c5f4b2a.

Source: product_association.py:163.

The exclusion looks accidental:

  • 10.0 is accepted, and filters correctly against an integer product_id column.
  • 3.5 is accepted, and returns an empty frame.
  • e2fcc3b ("Support str and float data types for target items") gives no reason for leaving out int.
  • The only test of the check (test_calc_association_target_item_invalid_type) passes a dict, not an int.

Expected: accept int and np.integer, alone or in a list. Related: #587 (annotation mismatches in this module).

Failing test (3 fail on main):

import numpy as np
import pandas as pd
import pytest

from openretailscience.analysis.product_association import ProductAssociation
from openretailscience.options import ColumnHelper

cols = ColumnHelper()


class TestProductAssociationIntegerTargetItem:
    @pytest.fixture
    def transactions_df(self) -> pd.DataFrame:
        return pd.DataFrame(
            {
                cols.transaction_id: [1, 1, 2, 2, 3],
                "product_id": [100234, 100877, 100234, 100951, 100877],
            },
        )

    @pytest.mark.parametrize("target_item", [100234, np.int64(100234), [100234]])
    def test_integer_target_item_filters_to_that_product(self, transactions_df, target_item):
        result = ProductAssociation(
            transactions_df,
            value_col="product_id",
            group_col=cols.transaction_id,
            target_item=target_item,
        ).df
        assert set(result["product_id_1"]) == {100234}
        assert set(result["product_id_2"]) == {100877, 100951}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions