Skip to content

TST/CI: Address enforced numpy DeprecationWarning in test_pandas_dtype_numpy_warning #60875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 7, 2025

Conversation

mroeschke
Copy link
Member

e.g.

=================================== FAILURES ===================================
_______________________ test_pandas_dtype_numpy_warning ________________________
[gw1] linux -- Python 3.11.11 /home/runner/micromamba/envs/test/bin/python3.11

    def test_pandas_dtype_numpy_warning():
        # GH#51523
        with tm.assert_produces_warning(
            DeprecationWarning,
            check_stacklevel=False,
            match="Converting `np.integer` or `np.signedinteger` to a dtype is deprecated",
        ):
>           pandas_dtype(np.integer)

pandas/tests/dtypes/test_common.py:796: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

dtype = <class 'numpy.integer'>

    def pandas_dtype(dtype) -> DtypeObj:
        """
        Convert input into a pandas only dtype object or a numpy dtype object.
    
        Parameters
        ----------
        dtype : object
            The object to be converted into a dtype.
    
        Returns
        -------
        np.dtype or a pandas dtype
            The converted dtype, which can be either a numpy dtype or a pandas dtype.
    
        Raises
        ------
        TypeError if not a dtype
    
        See Also
        --------
        api.types.is_dtype : Return true if the condition is satisfied for the arr_or_dtype.
    
        Examples
        --------
        >>> pd.api.types.pandas_dtype(int)
        dtype('int64')
        """
        # short-circuit
        if isinstance(dtype, np.ndarray):
            return dtype.dtype
        elif isinstance(dtype, (np.dtype, ExtensionDtype)):
            return dtype
    
        # builtin aliases
        if dtype is str and using_string_dtype():
            from pandas.core.arrays.string_ import StringDtype
    
            return StringDtype(na_value=np.nan)
    
        # registered extension types
        result = registry.find(dtype)
        if result is not None:
            if isinstance(result, type):
                # GH 31356, GH 54592
                warnings.warn(
                    f"Instantiating {result.__name__} without any arguments."
                    f"Pass a {result.__name__} instance to silence this warning.",
                    UserWarning,
                    stacklevel=find_stack_level(),
                )
                result = result()
            return result
    
        # try a numpy dtype
        # raise a consistent TypeError if failed
        try:
            with warnings.catch_warnings():
                # GH#51523 - Series.astype(np.integer) doesn't show
                # numpy deprecation warning of np.integer
                # Hence enabling DeprecationWarning
                warnings.simplefilter("always", DeprecationWarning)
>               npdtype = np.dtype(dtype)
E               TypeError: Converting 'np.integer' or 'np.signedinteger' to a dtype is not allowed

pandas/core/dtypes/common.py:1843: TypeError

During handling of the above exception, another exception occurred:

    def test_pandas_dtype_numpy_warning():
        # GH#51523
>       with tm.assert_produces_warning(
            DeprecationWarning,
            check_stacklevel=False,
            match="Converting `np.integer` or `np.signedinteger` to a dtype is deprecated",
        ):

pandas/tests/dtypes/test_common.py:791: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../../../micromamba/envs/test/lib/python3.11/contextlib.py:158: in __exit__
    self.gen.throw(typ, value, traceback)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

    def _assert_caught_expected_warnings(
        *,
        caught_warnings: Sequence[warnings.WarningMessage],
Warning:         expected_warning: type[Warning] | tuple[type[Warning], ...],
        match: str | None,
        check_stacklevel: bool,
    ) -> None:
        """Assert that there was the expected warning among the caught warnings."""
        saw_warning = False
        matched_message = False
        unmatched_messages = []
        warning_name = (
            tuple(x.__name__ for x in expected_warning)
            if isinstance(expected_warning, tuple)
            else expected_warning.__name__
        )
    
        for actual_warning in caught_warnings:
            if issubclass(actual_warning.category, expected_warning):
                saw_warning = True
    
                if check_stacklevel:
                    _assert_raised_with_correct_stacklevel(actual_warning)
    
                if match is not None:
                    if re.search(match, str(actual_warning.message)):
                        matched_message = True
                    else:
                        unmatched_messages.append(actual_warning.message)
    
        if not saw_warning:
>           raise AssertionError(f"Did not see expected warning of class {warning_name!r}")
E           AssertionError: Did not see expected warning of class 'DeprecationWarning'

pandas/_testing/_warnings.py:188: AssertionError

@mroeschke mroeschke added the Testing pandas testing functions or related to the test suite label Feb 7, 2025
@mroeschke mroeschke added this to the 2.3 milestone Feb 7, 2025
@mroeschke mroeschke changed the title TST: Address enforced numpy DeprecationWarning in test_pandas_dtype_numpy_warning TST/CI: Address enforced numpy DeprecationWarning in test_pandas_dtype_numpy_warning Feb 7, 2025
@mroeschke
Copy link
Member Author

Merging as the numpy/python-dev builds are passing now

@mroeschke mroeschke merged commit 5b16c06 into pandas-dev:main Feb 7, 2025
39 of 42 checks passed
@mroeschke mroeschke deleted the ci/numpy branch February 7, 2025 20:25
meeseeksmachine pushed a commit to meeseeksmachine/pandas that referenced this pull request Feb 7, 2025
…ionWarning in test_pandas_dtype_numpy_warning
mroeschke added a commit that referenced this pull request Feb 7, 2025
…precationWarning in test_pandas_dtype_numpy_warning) (#60878)

Backport PR #60875: TST/CI: Address enforced numpy DeprecationWarning in test_pandas_dtype_numpy_warning

Co-authored-by: Matthew Roeschke <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Testing pandas testing functions or related to the test suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant